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

  • 相关阅读:
    微信小程序 request请求封装
    JavaScript中使用比较多的两种创建对象的方式
    angularjs中audio/video 路径赋值问题
    ajax渲染swiper问题
    angularjs与vue循环数组对象是区别
    gulp安装搭建前端项目自动化
    vue中-webkit-box-orient:vertical打包放到线上不显示
    Redis高级客户端Lettuce详解
    redis单点、redis主从、redis哨兵 sentinel,redis集群cluster配置搭建与使用
    Java线程、线程池ThreadPoolExecutor详细剖析
  • 原文地址:https://www.cnblogs.com/zhouheblog/p/10653987.html
Copyright © 2011-2022 走看看