`

HttpClient V3、V4的常规设置及用法

 
阅读更多

本文出自:http://xiaofeng001.iteye.com/blog/982751

HttpClient V3和V4版本的区别还是蛮大的,没有细研究过V3,最近看了下V4,感觉很灵活,可定制性的东西很多。

=======================HttpClient3 属性设置========================
HttpClient client = new HttpClient();
HttpConnectionManagerParams cmp = client.getHttpConnectionManager().getParams();
cmp.setConnectionTimeout(5000);//http连接超时
cmp.setSoTimeout(5000);//读数据超时
client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");//编码

=======================HttpClient3 Post========================
PostMethod post = new PostMethod(url);
NameValuePair name$ = new NameValuePair("UserID","19104221");
NameValuePair authenticator$ = new NameValuePair("Authenticator",authenticator);
NameValuePair[] paramsPair = {name$,authenticator$};
post.addParameters(paramsPair);
int stateCode = client.executeMethod(post);
//TODO 处理响应
post.releaseConnection();
//post.abort();
((SimpleHttpConnectionManager)client.getHttpConnectionManager()).shutdown();


=======================HttpClient3 Get========================
GetMethod get = new GetMethod(url)
HttpMethodParams params = new HttpMethodParams();
params.setParameter("UserID", "99999005");
params.setParameter("Action", "Login");
get.setParams(params);
int statusCode = client.executeMethod(get);
//TODO 处理响应
get.releaseConnection();
//get.abort();
((SimpleHttpConnectionManager)client.getHttpConnectionManager()).shutdown();


========================HttpClient4属性设置===============================
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
httpclient.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);
httpclient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, charset == null ? CHARSET_GBK : charset);
httpclient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout);
httpclient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout);
//httpclient.setHttpRequestRetryHandler(requestRetryHandler);//请求重试Handler
//httpclient.setRedirectHandler();//重定向的处理(默认的DefaultRedirectHandler能够支持get,head自动重定向)
if(responseInterceptor!=null)
httpclient.addResponseInterceptor(responseInterceptor);//响应拦截器
if(requestInterceptor!=null)
httpclient.addRequestInterceptor(requestInterceptor);//请求拦截器

========================HttpClient4 Get===============================
List<NameValuePair> qparams = getParamsList(params);
if (qparams != null && qparams.size() > 0) {
charset = (charset == null ? CHARSET_GBK : charset);
String formatParams = URLEncodedUtils.format(qparams, charset);
url = (url.indexOf("?")) < 0 ? (url + "?" + formatParams) : (url
.substring(0, url.indexOf("?") + 1) + formatParams);
}
DefaultHttpClient httpClient = getDefaultHttpClient();
HttpGet hg = new HttpGet(url);
String response = httpClient.execute(hg,new BasicResponseHandler());//BasicResponseHandler控制响应结果为String
//TODO 处理响应
hg.abort();
httpClient.getConnectionManager().shutdown();
httpClient=null;

========================HttpClient4 Post===============================
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(getParamsList(params), charset);
DefaultHttpClient httpClient = getDefaultHttpClient();
HttpPost hp = new HttpPost(url);
if(formEntity!=null)
hp.setEntity(formEntity);
httpClient.execute(hp);
//TODO 处理响应
hg.abort();
httpClient.getConnectionManager().shutdown();
httpClient=null;

//Map结构的参数转为NameValuePair列表
public List<NameValuePair> getParamsList(Map<String, String> paramsMap) {
if (paramsMap == null || paramsMap.size() == 0) {
return null;
}
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> map : paramsMap.entrySet()) {
params.add(new BasicNameValuePair(map.getKey(), map.getValue()));
}
return params;
}


分享到:
评论

相关推荐

    httpclient用法,发送get和post请求,设置header

    httpclient的用法,发送get请求和post请求,设置header

    Httpclient使用jar包三合一,基本使用方法

    简单使用方法: public static void main(String[] args) { // String str1 = &quot;http://dev.d-smart.cn/Login&quot;; // http协议路径 String str1 = &quot;&quot;; HttpClient httpClient = new ...

    httpclient4.3 设置代理

    javase http通讯技术 apache httpclient4.3 设置代理详解

    HttpClient用法,实例

    HttpClient用法,实例 HttpClient用法,实例 HttpClient用法,实例 HttpClient用法,实例

    httpclient-4.5.6-API文档-中文版.zip

    赠送jar包:httpclient-4.5.6.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    HttpClient 基本用法

    HttpClient 基本用法,HttpClient 基本用法

    httpClient

    * 使用 GetMethod 来访问一个 URL 对应的网页,实现步骤: 1:生成一个 HttpClinet 对象并设置相应的参数。 * 2:生成一个 GetMethod 对象并设置响应的参数。 3:用 HttpClinet 生成的对象来执行 GetMethod 生成的Get ...

    httpclient

    使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。 1. 创建HttpClient对象。 2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建...

    HttpClient用法

    HttpClient获得客户端信息(XML),返回服务器一个地址类似于:http://download.csdn.net/download/yonsin/4129984

    Common-httpClient各个版本jar及源码

    用快压解压 Common-httpClient各个版本jar及源码

    HttpClient各种使用方法

    NULL 博文链接:https://wenkaixuan.iteye.com/blog/2109590

    httpclient httpclient.jar

    httpClient完整封装获取网页信息、数据的代码+httpclient.jar

    httpclient-4.5.5-API文档-中文版.zip

    赠送jar包:httpclient-4.5.5.jar;...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    Java后端HttpClient Post提交文件流 及服务端接收文件流

    HttpClient Post提交多文件及多个普通参数,已经封装成工具类。 需传入 要请求的url 普通参数map 例 map.put("param1","张三"); 需要传入的文件流map 其中key为文件名 服务端接收无乱码。

    HttpClient工具类封装.docx

    HttpClient工具类封装,基于httpclient4.5.12,有get、post、put、delete方法,可设置连接超时时间,请求超时时间,socket读写超时时间,设置是否允许重定向,字符集

    HttpClient包及例子

    简单来说HttpClient就是模拟post或get的. 简化HTTP客户端与服务器进行各种通讯的开源东东 最新是4.0-alpha2,内含jar包及例子.

Global site tag (gtag.js) - Google Analytics