zoukankan      html  css  js  c++  java
  • HttpClient基础教程 分类: C_OHTERS 2014-05-18 23:23 2600人阅读 评论(0) 收藏


    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方法直接访问网站。


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    【原创】【1】rich editor系列教程。前期准备,兼容
    html5 video,audio控制播放多次,请求/监测全屏状态
    javascript中的位运算,
    温习classList api
    Fiddler常用功能总结
    小程序测试点总结
    Pycharm使用git版本控制
    python 运行当前目录下的所有文件
    MySQL查询count(*)、count(1)、count(field)的区别收集
    python 操作redis,存取为字节格式,避免转码加
  • 原文地址:https://www.cnblogs.com/lujinhong2/p/4637337.html
Copyright © 2011-2022 走看看