httpclient4.3下载远程图片,设置user-agent和伪装成google的refer, 并设置timeout.
首先maven中加入jar依赖关系
<!-- lock httpclient version --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.10</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency>
package com.mkyong.common.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ImageDownloader { private static final String USER_AGENT = "Mozilla/5.0 Firefox/26.0"; private static Logger logger = LoggerFactory.getLogger(ImageDownloader.class); private static final int TIMEOUT_SECONDS = 120; private static final int POOL_SIZE = 120; private static CloseableHttpClient httpclient; public static void main(String[] args) throws ClientProtocolException, IOException { ImageDownloader imageDownloader = new ImageDownloader(); imageDownloader.initApacheHttpClient(); String imageUrl = "https://ibclaty.j.com/niku/ui/uitk/images/s.gif"; imageDownloader.fetchContent(imageUrl); imageDownloader.destroyApacheHttpClient(); } public void initApacheHttpClient() { // Create global request configuration RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(TIMEOUT_SECONDS * 1000) .setConnectTimeout(TIMEOUT_SECONDS * 1000).build(); // Create an HttpClient with the given custom dependencies and // configuration. httpclient = HttpClients.custom().setUserAgent(USER_AGENT).setMaxConnTotal(POOL_SIZE) .setMaxConnPerRoute(POOL_SIZE).setDefaultRequestConfig(defaultRequestConfig).build(); } private void destroyApacheHttpClient() { try { httpclient.close(); } catch (IOException e) { logger.error("httpclient close fail", e); } } public void fetchContent(String imageUrl) throws ClientProtocolException, IOException { HttpGet httpget = new HttpGet(imageUrl); httpget.setHeader("Referer", "http://www.google.com"); System.out.println("executing request " + httpget.getURI()); CloseableHttpResponse response = httpclient.execute(httpget); try { HttpEntity entity = response.getEntity(); if (response.getStatusLine().getStatusCode() >= 400) { throw new IOException("Got bad response, error code = " + response.getStatusLine().getStatusCode() + " imageUrl: " + imageUrl); } if (entity != null) { InputStream input = entity.getContent(); OutputStream output = new FileOutputStream(new File("D:\\website\\1.gif")); IOUtils.copy(input, output); output.flush(); } } finally { response.close(); } } }
相关推荐
这个压缩包包含了HttpClient4.3中文版pdf和英文版pdf 。word文档里也有几个学习的网址。如果用HttpClient4.5的话上面的知识基本(95%)能用的上。省去了到处找零碎资料的时间。
httpclient4.3的二次封装,解决了代理问题、多线程问题、SSL问题、通用的get、post请求
httpclient4.3 和 httpcore4.4httpclient4.3 和 httpcore4.4httpclient4.3 和 httpcore4.4
httpclient4.3开发的工具类,httpclientUtils 因为项目需要,自己就根据常用的写了一个。
httpClient4.3的jar包
httpclient4.3所需jar包
httpClient4.3官方包,官方源文件,可以直接使用
提供HttpClient4.3所需要的jar
压缩包内包括 httpclient 4.3的jar包和完整的demo程序。想学httpclient 4的赶紧下啊。
httpclient4.3.x和其依赖jar包。
httpclient-4.3.jar apache http 欢迎下载
javase http通讯技术 apache httpclient4.3 设置代理详解
图片上传需要的三个jar包(commons-codec-1.3.jar,commons-httpclient-3.1.jar,commons-logging-1.1.jar)
httpclient中文版本,HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。本文首先介绍 ...
httpclient-4.3-beta1
httpcore-4.2.4,httpclient-4.2.5,httpclient-cache-4.2.5,httpmime-4.2.5的jar包下载。 比如微信退款需要以上jar包
HTTPclient 4.3版本的Apache官网英文版的翻译版本
Apache Httpclient4.3.x 系列jar套件
最新版httpclient自动登录人人代码,内含有代码,httpclient4.3包,只需要修改账户和密码,就可以登陆
NULL 博文链接:https://chun521521.iteye.com/blog/2422270