HttpClient3、4基本用法

更新时间:2023-05-23 09:19:38 阅读: 评论:0

=======================HttpClient3 属性设置========================
harbingerHttpClient client = new HttpClient();
HttpConnectionManagerParams cmp = HttpConnectionManager().getParams();
cmp.tConnectionTimeout(5000);//http连接超时
cmp.tSoTimeout(5000);//读数据超时
=======================HttpClient3 Post========================
PostMethod post = new PostMethod(url);
NameValuePair name$ = new NameValuePair("UrID","19104221");
NameValuePair authenticator$ = new NameValuePair("Authenticator",authenticator);
NameValuePair[] paramsPair = {name$,authenticator$};
post.addParameters(paramsPair);
英文翻译在线
schint stateCode = uteMethod(post);
//TODO 处理响应
//post.abort();
((HttpConnectionManager()).shutdown();
=======================HttpClient3 Get========================
GetMethod get = new GetMethod(url)
HttpMethodParams params = new HttpMethodParams();
params.tParameter("UrID", "99999005");
params.tParameter("Action", "Login");
get.tParams(params);
int statusCode = uteMethod(get);
//TODO 处理响应
//get.abort();
((HttpConnectionManager()).shutdown();
========================HttpClient4属性设置===============================
DefaultHttpClient httpclient = new DefaultHttpClient();
//模拟浏览器,解决一些服务器程序只允许浏览器访问的问题
//httpclient.tHttpRequestRetryHandler(requestRetryHandler);//请求重试Handler
//httpclient.tRedirectHandler();//重定向的处理(默认的DefaultRedirectHandler能够支持get,head自动重定向)
if(responInterceptor!=null)
httpclient.addResponInterceptor(responInterceptor);//响应拦截器
if(requestInterceptor!=null)
httpclient.addRequestInterceptor(requestInterceptor);//请求拦截器
========================HttpClient4 Get===============================
List<NameValuePair> qparams = getParamsList(params);
if (qparams != null && qparams.size() > 0) {
chart = (chart == null ? CHARSET_GBK : chart);
perverance
String formatParams = URLEncodedUtils.format(qparams, chart);
url = (url.indexOf("?")) < 0 ? (url + "?" + formatParams) : (url
.substring(0, url.index
Of("?") + 1) + formatParams);
}
DefaultHttpClient httpClient = getDefaultHttpClient();
HttpGet hg = new HttpGet(url);男士穿衣打扮
String respon = ute(hg,new BasicResponHandler());//BasicResponHandler控制响应结果为String
//TODO 处理响应
lookforwardtohg.abort();
va读音
httpClient=null;
========================HttpClient4 Post===============================
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(getParamsList(params), chart);
新东方英语暑假班DefaultHttpClient httpClient = getDefaultHttpClient();
HttpPost hp = new HttpPost(url);
if(formEntity!=null)
hp.tEntity(formEntity);
//TODO 处理响应
hg.abort();
httpClient=null;
//Map结构的参数转为NameValuePair列表
protected List<NameValuePair> getParamsList(Map<String, String> paramsMap) {itu
if (paramsMap == null || paramsMap.size() == 0) {
etn
return null;
}
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> map : Set()) {
params.add(new Key(), Value()));
}
return params;
}

本文发布于:2023-05-23 09:19:38,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/743335.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:响应   处理   浏览器   默认   转为   参数   允许
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图