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;
}