一、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(); } }
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/285781.html