Java HttpClient使用小结
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java HttpClient使用小结,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4575字,纯文字阅读大概需要7分钟。
内容图文
httpclient是apache的一个项目:http://hc.apache.org/
文档比較完好:http://hc.apache.org/httpcomponents-client-ga/tutorial/html/
这里就不啰嗦了,主要是在做demo的时候遇到的一些问题在这里总结一下:
[引用请注明出处http://blog.csdn.net/bhq2010/article/details/9210007]
1、使用连接池
虽说http协议时无连接的,但毕竟是基于tcp的,底层还是须要和server建立连接的。对于须要从同一个网站抓取大量网页的程序,应该使用连接池,否则每次抓取都和Web网站建立连接、发送请求、获得响应、释放连接,一方面效率不高,还有一方面稍不小心就会疏忽了某些资源的释放、导致网站拒绝连接(非常多网站会拒绝同一个ip的大量连接、防止DOS攻击)。
连接池的例程例如以下:
SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry); cm.setMaxTotal(200); cm.setDefaultMaxPerRoute(2); HttpHost googleResearch = new HttpHost("research.google.com", 80); HttpHost wikipediaEn = new HttpHost("en.wikipedia.org", 80); cm.setMaxPerRoute(new HttpRoute(googleResearch), 30); cm.setMaxPerRoute(new HttpRoute(wikipediaEn), 50);
SchemaRegistry的作用是注冊协议的默认port号。PoolingClientConnectionManager是池化连接管理器,即连接池,setMaxTotal设置连接池的最大连接数,setDefaultMaxPerRoute设置每一个路由(http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d5e467)上的默认连接个数,setMaxPerRoute则单独为某个网站设置最大连接个数。
从连接池中获取http client也非常方面:
DefaultHttpClient client = new DefaultHttpClient(cm);
2、设置HttpClient參数
HttpClient须要设置合适的參数,才干更好地工作。默认的參数可以应付少量的抓取工作,但找到一组合适的參数往往能改善特定情况下的抓取效果。设置參数的例程例如以下:
DefaultHttpClient client = new DefaultHttpClient(cm); Integer socketTimeout = 10000; Integer connectionTimeout = 10000; final int retryTime = 3; client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, socketTimeout); client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectionTimeout); client.getParams().setParameter(CoreConnectionPNames.TCP_NODELAY, false); client.getParams().setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 1024 * 1024); HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount >= retryTime) { // Do not retry if over max retry count return false; } if (exception instanceof InterruptedIOException) { // Timeout return false; } if (exception instanceof UnknownHostException) { // Unknown host return false; } if (exception instanceof ConnectException) { // Connection refused return false; } if (exception instanceof SSLException) { // SSL handshake exception return false; } HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if (idempotent) { // Retry if the request is considered idempotent return true; } return false; } }; client.setHttpRequestRetryHandler(myRetryHandler);
3、设置request header
HttpResponse response = null; HttpGet get = new HttpGet(url); get.addHeader("Accept", "text/html"); get.addHeader("Accept-Charset", "utf-8"); get.addHeader("Accept-Encoding", "gzip"); get.addHeader("Accept-Language", "en-US,en"); get.addHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.160 Safari/537.22"); response = client.execute(get); HttpEntity entity = response.getEntity(); Header header = entity.getContentEncoding(); if (header != null) { HeaderElement[] codecs = header.getElements(); for (int i = 0; i < codecs.length; i++) { if (codecs[i].getName().equalsIgnoreCase("gzip")) { response.setEntity(new GzipDecompressingEntity(entity)); } } } return response;
各个header的含义參考http://kb.cnblogs.com/page/92320/
原文:http://www.cnblogs.com/lcchuguo/p/4004071.html
内容总结
以上是互联网集市为您收集整理的Java HttpClient使用小结全部内容,希望文章能够帮你解决Java HttpClient使用小结所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。