一、httpclient是什么
HttpClient是一个开源的Java库,它支持HTTP协议。它总共只分为四个模块:HttpClient是模块的核心,用于提供httpclient的api, HttpCore提供基本的网络操作,至于协议相关的内容可以去Apache的官网查阅,HttpAsyncClient是异步的httpclient,HttpMime用于处理网络数据的mime类型。
二、使用httpclient下载文件的步骤
使用httpclient下载文件要经过以下几个步骤:
1、创建HttpClient对象
2、创建HttpGet或HttpPost请求对象
3、执行请求,获得响应结果
4、如果响应的状态码是200,说明响应成功,获取响应的输入流并读取数据
5、关闭输入流和HttpClient对象。
三、httpclient下载文件的示例代码
import java.io.*; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; public class HttpDownload{ public static void main(String[] args) throws Exception{ String url = "http://www.example.com/file/example.jpg"; HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(httpget); if(response.getStatusLine().getStatusCode() == 200){ InputStream is = response.getEntity().getContent(); File file = new File("example.jpg"); FileOutputStream fos = new FileOutputStream(file); byte[] buffer = new byte[1024]; int len = 0; while((len = is.read(buffer))!=-1){ fos.write(buffer, 0, len); } fos.close(); is.close(); } httpclient.getConnectionManager().shutdown(); } }
四、设置httpclient的请求头
在http请求中,请求头的内容是非常重要的。对于一些网站,它们可以根据请求头来判断请求的来源,并进行相应的处理。在使用httpclient进行文件下载时,也可以设置请求头,以达到隐藏下载行为的目的。
为了设置请求头,我们可以使用httpget.setHeader方法。下面是示例代码:
import java.io.*; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; public class HttpDownload{ public static void main(String[] args) throws Exception{ String url = "http://www.example.com/file/example.jpg"; HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url); httpget.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); HttpResponse response = httpclient.execute(httpget); if(response.getStatusLine().getStatusCode() == 200){ InputStream is = response.getEntity().getContent(); File file = new File("example.jpg"); FileOutputStream fos = new FileOutputStream(file); byte[] buffer = new byte[1024]; int len = 0; while((len = is.read(buffer))!=-1){ fos.write(buffer, 0, len); } fos.close(); is.close(); } httpclient.getConnectionManager().shutdown(); } }
五、设置httpclient的请求参数
如果您需要带上一些请求参数,以便进行授权或其它的认证,就可以使用httpclient的HttpParams类。HttpParams类是一个接口,它的实现类是BasicHttpParams。
这里的请求参数包括Timeout、socketBufferSize、http.socket.timeout、http.connection.timeout、http.connection-manager.timeout。下面是示例代码:
import java.io.*; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.PoolingClientConnectionManager; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.params.HttpParams; import org.apache.http.params.BasicHttpParams; public class HttpDownload{ public static void main(String[] args) throws Exception{ String url = "http://www.example.com/file/example.jpg"; HttpParams params = new BasicHttpParams(); params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 1000); params.setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 1024); params.setParameter("http.socket.timeout", new Integer(1000)); params.setParameter("http.connection.timeout", new Integer(1000)); params.setParameter("http.connection-manager.timeout", new Long(1000)); HttpClient httpclient = new DefaultHttpClient(new PoolingClientConnectionManager(), params); HttpGet httpget = new HttpGet(url); httpget.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); HttpResponse response = httpclient.execute(httpget); if(response.getStatusLine().getStatusCode() == 200){ InputStream is = response.getEntity().getContent(); File file = new File("example.jpg"); FileOutputStream fos = new FileOutputStream(file); byte[] buffer = new byte[1024]; int len = 0; while((len = is.read(buffer))!=-1){ fos.write(buffer, 0, len); } fos.close(); is.close(); } httpclient.getConnectionManager().shutdown(); } }