`
diddyrock
  • 浏览: 45291 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

Httpclient4.0使用

阅读更多
3.0有时候会有connection timing bug
the situation has not happened under 4.0
this is how to step forward to the 4.0 edition
不过4.0设计的不如3.0爽阿,包结构太复杂,源代码看起来很费劲,折腾了一天,留此
以记
:


this.params = new BasicHttpParams();
ConnManagerParams.setMaxTotalConnections(params, 5);
ConnManagerParams.setTimeout(params, TIME_OUT);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");


ArrayList headers = new ArrayList();
headers.add(new BasicHeader("User-Agent", FIRE_FOX_AGENT_WIN));
headers.add(new BasicHeader("Accept", "*/*"));
headers.add(new BasicHeader("Accept-Encoding", "x-gzip, gzip, deflate"));
headers.add(new BasicHeader("Accept-Language", "zh-cn,zh;q=0.5"));
headers.add(new BasicHeader("Accept-Charset", "gb2312,utf-8,ISO-8859-1;q=0.7,*;q=0.7"));
params.setParameter(ClientPNames.DEFAULT_HEADERS, headers);


HttpClientParams.setCookiePolicy(params, CookiePolicy.BROWSER_COMPATIBILITY);

HttpConnectionParams.setSoTimeout(params, 3000);
     HttpConnectionParams.setSocketBufferSize(params, 8*1024);
     HttpConnectionParams.setConnectionTimeout(params, 3000);
     params.setBooleanParameter(CookieSpecPNames.SINGLE_COOKIE_HEADER, true);
this.schemeRegistry = new SchemeRegistry();
this.schemeRegistry.register(
                new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
     this.schemeRegistry.register(
                new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
     this.cm = new ThreadSafeClientConnManager(this.params, this.schemeRegistry);
     this.httpClient = new DefaultHttpClient(this.cm, this.params);
HttpGet httpGet = new HttpGet(url);

HttpParams params = httpClient.getParams();
//params.getBooleanParameter(CookieSpecPNames.SINGLE_COOKIE_HEADER, false);
Header referer = new BasicHeader("Referer",url);
HttpResponse hre = httpClient.execute(httpGet,localContext);
//Header[] heads = httpClient.
Header[] heads = ((RequestWrapper)localContext.getAttribute(ExecutionContext.HTTP_REQUEST)).getAllHeaders();
System.out.println("request +++++++++++++++++++++++++++++++++++++");
for(int i=0;i<heads.length;i++){
System.out.println(heads[i].getName()+": "+heads[i].getValue());
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics