zoukankan      html  css  js  c++  java
  • HttpClient基本使用

    1.在pom.xml加入对httpclient的必需的jar包的依赖

            <!--//httpclient的接口基本都在这儿-->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient-cache</artifactId>
                <version>4.5</version>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpmime</artifactId>
                <version>4.3.2</version>
            </dependency>

    注意:常见的MIME类型(通用型):

        超文本标记语言文本 .html text/html

        xml文档 .xml text/xml

        XHTML文档 .xhtml application/xhtml+xml

        普通文本 .txt text/plain

        RTF文本 .rtf application/rtf

        PDF文档 .pdf application/pdf

        Microsoft Word文件 .word application/msword

        PNG图像 .png image/png

        GIF图形 .gif image/gif

        JPEG图形 .jpeg,.jpg image/jpeg

        au声音文件 .au audio/basic

        MIDI音乐文件 mid,.midi audio/midi,audio/x-midi

        RealAudio音乐文件 .ra, .ram audio/x-pn-realaudio

        MPEG文件 .mpg,.mpeg video/mpeg

        AVI文件 .avi video/x-msvideo

        GZIP文件 .gz application/x-gzip

        TAR文件 .tar application/x-tar

        任意的二进制数据 application/octet-stream

    2.抓取网页的内容并打印到控制台的demo

    package com.zhouhe.modules.api.test;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    import org.apache.http.HttpEntity;
    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.apache.http.util.EntityUtils;
    
    /**
     * Created by zhouhe on 2019/3/14 8:52
     */
    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String url="http://www.baidu.com";
    
    
            //1.使用默认的配置的httpclient
            CloseableHttpClient client = HttpClients.createDefault();
            //2.使用get方法
            HttpGet httpGet = new HttpGet(url);
            InputStream inputStream = null;
            CloseableHttpResponse response = null;
    
            try {
                //3.执行请求,获取响应
                response = client.execute(httpGet);
    
    
                //看请求是否成功,这儿打印的是http状态码
                System.out.println(response.getStatusLine().getStatusCode());
                //4.获取响应的实体内容,就是我们所要抓取得网页内容
                HttpEntity entity = response.getEntity();
    
                //5.将其打印到控制台上面
                //方法一:使用EntityUtils
                if (entity != null) {
                    System.out.println(EntityUtils.toString(entity, "utf-8"));
                }
                EntityUtils.consume(entity);
    
                //方法二  :使用inputStream
               /* if (entity != null) {
                    inputStream = entity.getContent();
    
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    String line = "";
                    while ((line = bufferedReader.readLine()) != null) {
                        System.out.println(line);
    
                    }
                }*/
    
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if (response != null) {
                    try {
                        response.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
    
            }
    
        }
    }

    原文网址:

    https://www.cnblogs.com/LuckyBao/p/6096145.html

  • 相关阅读:
    noi 2011 noi嘉年华 动态规划
    最小乘积生成树
    noi 2009 二叉查找树 动态规划
    noi 2010 超级钢琴 划分树
    noi 2011 阿狸的打字机 AC自动机
    noi 2009 变换序列 贪心
    poj 3659 Cell Phone Network 动态规划
    noi 2010 航空管制 贪心
    IDEA14下配置SVN
    在SpringMVC框架下建立Web项目时web.xml到底该写些什么呢?
  • 原文地址:https://www.cnblogs.com/zhouheblog/p/10653987.html
Copyright © 2011-2022 走看看