HttpClient基础教程


1、HttpClient相关的重要资料

官方网站:http://hc.apache.org/

API:http://hc.apache.org/httpcomponents-client-4.3.x/httpclient/apidocs/index.html

tutorial: http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/index.html  【PDF版本】http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/pdf/httpclient-tutorial.pdf


2、HttpClient有2个版本

org.apache.http.impl.client.HttpClients 与 org.apache.commons.httpclient.HttpClient

目前后者已被废弃,apache已不再支持。

一般而言,使用HttpClient均需导入httpclient.jar与httpclient-core.jar2个包。


3、使用HttpClient进行网络处理的基本步骤

(1)通过get的方式获取到Response对象。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. CloseableHttpClient httpClient = HttpClients.createDefault();  
  2. HttpGet httpGet = new HttpGet("http://www.baidu.com/");  
  3. CloseableHttpResponse response = httpClient.execute(httpGet);  

注意,必需要加上http://的前缀,否则会报:Target host is null异常。


(2)获取Response对象的Entity。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. HttpEntity entity = response.getEntity();  

注:HttpClient将Response的正文及Request的POST/PUT方法中的正文均封装成一个HttpEntity对象。可以通过entity.getContenType(),entity.getContentLength()等方法获取到正文的相关信息。但最重要的方法是通过getContent()获取到InputStream对象。

(3)通过Entity获取到InputStream对象,然后对返回内容进行处理。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. is = entity.getContent();  
  2. sc = new Scanner(is);  
  3. // String filename = path.substring(path.lastIndexOf('/')+1);  
  4. String filename = "2.txt";  
  5. os = new PrintWriter(filename);  
  6. while (sc.hasNext()) {  
  7.     os.write(sc.nextLine());  
  8. }  

使用HtppClient下载一个网页的完整代码如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.ljh.test;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.io.PrintWriter;  
  6. import java.io.Writer;  
  7. import java.util.Scanner;  
  8.   
  9. import org.apache.http.HttpEntity;  
  10. import org.apache.http.HttpStatus;  
  11. import org.apache.http.client.ClientProtocolException;  
  12. import org.apache.http.client.methods.CloseableHttpResponse;  
  13. import org.apache.http.client.methods.HttpGet;  
  14. import org.apache.http.impl.client.CloseableHttpClient;  
  15. import org.apache.http.impl.client.HttpClients;  
  16.   
  17. public class DownloadWebPage{  
  18.   
  19.     public static void downloadPagebyGetMethod() throws IOException {  
  20.   
  21.         // 1、通过HttpGet获取到response对象  
  22.         CloseableHttpClient httpClient = HttpClients.createDefault();  
  23.         HttpGet httpGet = new HttpGet("http://www.baidu.com/");  
  24.         CloseableHttpResponse response = httpClient.execute(httpGet);  
  25.   
  26.         InputStream is = null;  
  27.         Scanner sc = null;  
  28.         Writer os = null;  
  29.         if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
  30.             try {  
  31.                 // 2、获取response的entity。  
  32.                 HttpEntity entity = response.getEntity();  
  33.   
  34.                 // 3、获取到InputStream对象,并对内容进行处理  
  35.                 is = entity.getContent();  
  36.                 sc = new Scanner(is);  
  37.                 // String filename = path.substring(path.lastIndexOf('/')+1);  
  38.                 String filename = "2.txt";  
  39.                 os = new PrintWriter(filename);  
  40.                 while (sc.hasNext()) {  
  41.                     os.write(sc.nextLine());  
  42.                 }  
  43.   
  44.             } catch (ClientProtocolException e) {  
  45.                 e.printStackTrace();  
  46.             } finally {  
  47.                 if (sc != null) {  
  48.                     sc.close();  
  49.                 }  
  50.                 if (is != null) {  
  51.                     is.close();  
  52.                 }  
  53.                 if (os != null) {  
  54.                     os.close();  
  55.                 }  
  56.                 if (response != null) {  
  57.                     response.close();  
  58.                 }  
  59.             }  
  60.         }  
  61.   
  62.     }  
  63.   
  64.     public static void main(String[] args) {  
  65.         try {  
  66.             downloadPagebyGetMethod();  
  67.         } catch (IOException e) {  
  68.             e.printStackTrace();  
  69.         }  
  70.     }  
  71.   
  72. }  

注意:直接将HttpGet改为HttpPost,返回的结果有误,百度返回302状态,即重定向,新浪返回拒绝访问。怀疑大多网站均不允许POST方法直接访问网站。


原文地址:https://www.cnblogs.com/jediael/p/4304148.html