HttpConnection的使用
项目中需要与第三方系统交互,而交互的方式是XML报文形式,所以会用到HttpConnection与第三方系统连接交互,使用起来并不复杂,但是有几点需要注意的:
1.乱码的问题解决
2.超时的设置,注意这个问题很严重,当你网络正常的时候看不出区别,但是当你网络不正常的时候,没有设置超时时间会导致你的系统一直尝试连接三方系统,可能会导致系统延迟很久所以一定记得处理,一个应用的效率很重要。
附上代码:
HttpURLConnection urlConnection = null;
OutputStream outputStream = null;
BufferedReader bufferedReader = null; try { URL httpUrl = new URL(url);//创建对应的url对象
urlConnection = (HttpURLConnection) httpUrl.openConnection();//HttpConnection对象,这一步只是创建了对象并没有连接远程服务器
urlConnection.setDoInput(true);//允许读
urlConnection.setDoOutput(true);//允许写
urlConnection.setRequestMethod("POST");//请求方式
urlConnection.setRequestProperty("Pragma:", "no-cache");
urlConnection.setRequestProperty("Cache-Control", "no-cache");
urlConnection.setRequestProperty("Content-Type", "text/xml");//请求的消息格式
urlConnection.setConnectTimeout(6000);//很重要,设置超时时间
urlConnection.connect();//真正的连接服务器
outputStream = urlConnection.getOutputStream();
byte[] bytes = xml.getBytes("GBK");//这里的xml即为你想传递的值,因为项目中与三方交互要用GBK编码所以转换为GBK
outputStream.write(bytes);//传递值
StringBuffer temp = new StringBuffer();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
Reader rd = new InputStreamReader(in,"GBK");//服务器返回的也是GBK编码格式的数据。
int c = 0;
while ((c = rd.read()) != -1) {
temp.append((char) c);
}//其实可以使用String str =new String(response.getBytes(),"GBK");这种方式解决乱码问题,但是这次项目中使用这种方式并没有解决,所以干脆一个个字节的转。
in.close();
return temp.toString(); } catch (MalformedURLException e) {
log.error("connect server failed,cause{}", e.getMessage()); } catch (IOException e) {
log.error("io execute failed,cause{}", e.getMessage());
throw e; } finally {
try { if (!Arguments.isNull(outputStream)) {
outputStream.close();
}
if (!Arguments.isNull(bufferedReader)) {
bufferedReader.close();
} //该处理的资源需要处理掉,该关闭的需要关闭
} catch (IOException e) {
log.error("io close failed , cause{}", e.getMessage());
} }
交互很简单 但是细节很重要。。。。
HttpConnection的使用的更多相关文章
- HttpConnection方式访问网络
参考疯狂android讲义,重点在于学习1.HttpConnection访问网络2.多线程下载文件的处理 主activity: package com.example.multithreaddownl ...
- 解决ImportError: cannot import name HTTPConnection的方法
在写python程序的时候,使用from httplib import HTTPConnection,在run的时候提示ImportError: cannot import name HTTPConn ...
- python httpConnection详解
模块urllib,urllib2,httplib的区别 httplib实现了http和https的客户端协议,但是在python中,模块urllib和urllib2对httplib进行了更上层的封装. ...
- HttpConnection
1.HttpConnection 用于接受和发送网络数据 网络操作必须新开个子线程执行,否则会出现 ANR(Application Not Response) 应用无响应异常 Get: /** * 通 ...
- HTTPConnectionPool(host='xx.xx.xx.xx', port=xx): Max retries exceeded with url:(Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x0000015A25025EB8>...))
HTTPConnectionPool(host='xx.xx.xx.xx', port=xx): Max retries exceeded with url:(Caused by ConnectTim ...
- HttpConnection详解【转】
HttpConnection详解[转] HttpURLConnection对象 1.从Internet获取网页,发送请求,将网页以流的形式读回来. 步骤:1)创建一个URL对象:URL url ...
- httplib:AttributeError: 'module' object has no attribute 'HTTPConnection'
# -*-coding:gb2312-*- #Function:学习python的httplib模块 import httplib conn = httplib.HTTPConnection(&quo ...
- python urllib2 httplib HTTPConnection
httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现. import httplib conn ...
- Android使用Http协议访问网络——HttpConnection
套路篇 使用HttpConnection访问网络一般有如下的套路: 1.获取到HttpConnection的实例,new出一个URL对象,并传入目标的网址,然后调用一下openConnection() ...
随机推荐
- NGS中的一些软件功能介绍
1.bowtie 短序列比对工具,blast也是短序列比对工具,速度快,结果易理解. 输入可以是fastq或者fasta文件. 生成比对结果文件sam格式的吧. 2.bwa 转自:https://ww ...
- Spring框架第六篇之Spring与DAO
一.Spring与JDBC模板 1.搭建环境 首先导入需要的jar包: 以上jar中多导入了DBCP和C3P0的jar包,因为这里需要演示怎么配置多种数据源,所以导入了这两个包,在实际开发中无需导入这 ...
- 创建发布Webservice以及wsimport工具
一. 通过wsimport生成本地代理调用WebService 1.推荐的访问服务方式 WebService已纳入w3c规范,其他的平台都支持该规范 :J2EE\Php\.NET都支持wsimport ...
- android 显示internet 图片
try { HttpGet httpRequest = new HttpGet(edtUrl.getText() .toString()); HttpClient httpclient = new D ...
- 关于cgi、FastCGI、php-fpm、php-cgi(复制)
首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. web server(比如说nginx)只是内容的分发者.比如,如果请求/index.h ...
- PKU 4334 Trouble(哈希)
原题链接 思路:哈希存入相反数 注意:HDU不支持long long要使用__int64 #include<cstdio> #include<cstring> #define ...
- linux下创建用户,给用户设置密码,给用户授权
1.linux下的用户是属于组的,所以需要创建一个组,划分给用户.创建命令: 在root下执行 groupadd ver 创建一个组ver 2.创建用户 useradd ...
- .NET Core + EF 报nuget包不兼容
错误信息如下: 严重性 代码 说明 项目 文件 行 禁止显示状态错误 NU1107 Microsoft.EntityFrameworkCore 中检测到版本冲突.直接安装/引用 Microsoft.E ...
- 用74HC165读8个按键状态
源:用74HC165读8个按键状态 源:74LV165与74HC595 使用 74LV165说明: 74LV165是8位并行负载或串行输入移位寄存器,末级提供互补串行输出(Q7和Q7).并行负载(PL ...
- 分享基于Sails.js和React.js的全栈聊天室
在线地址: http://fiora.suisuijiang.com/ 项目源码[ 路过求star(^_^) ]: 后端: https://github.com/yinxin630/fiora-bac ...