HttpClient入门get post请求(抓取网页的内容并打印到控制台)

1.HttpClient入门使用

        注意这个版本主要是基于HttpClient4.5.2版本的来讲解的,也是现在最新的版本,之所以要提供版本说明的是因为HttpClient 3版本和HttpClient 4版本差别还是很多大的,基本HttpClient里面的接口都变了,你把HttpClient 3版本的代码拿到HttpClient 4上面都运行不起来,会报错的。所以这儿一定要注意,好了废话不多说了,开始。

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



    org.apache.httpcomponents
    httpclient
    4.5.2


    org.apache.httpcomponents
    httpclient-cache
    4.5


    org.apache.httpcomponents
    httpmime
    4.3.2

注意:常见的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

3.抓取网页的内容并打印到控制台的demo--get请求

复制代码

    @Test
    public void testHttpClientA() throws IOException {
        //使用默认配置的httpclient
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //即将访问的url
        String url = "http://www.baidu.com";
        //get形式的访问
        HttpGet httpGet = new HttpGet(url);

        //执行请求
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            //打印请求的状态码  请求成功为200
            System.out.println(response.getStatusLine().getStatusCode());
            //打印请求的实体内容 返回json格式
            HttpEntity entity = response.getEntity();
            //获取所有头信息
            Header[] allHeaders = response.getAllHeaders();
            for (Header allHeader : allHeaders) {
                System.out.println(allHeader.getName());
                System.out.println(allHeader.getValue());
                System.out.println(allHeader.toString());
            }

            //方法一 官方不推荐
            if (entity != null) {
                //输出更详细的抓取内容(html格式)
              System.out.println(EntityUtils.toString(entity,"utf-8"));
            }
            //释放资源
            EntityUtils.consume(entity);
            //方法二 官方推荐 使用流的形式处理请求结果
      /*  if (entity != null) {
            InputStream content = entity.getContent();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(content));
            String line = "";
            while ((line = bufferedReader.readLine()) != null){
                System.out.println(line);
            }
            bufferedReader.close();
        }*/
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            response.close();
        }

复制代码

4.带参数的请求--get请求

复制代码

    @Test
    public void testHttpClientB() throws URISyntaxException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        URI uri = new URIBuilder()
                .setScheme("http")
                .setHost("www.google.com")
                .setPath("/search")
                .setParameter("q", "httpclient")
                .setParameter("btnG", "Google搜索")
                .setParameter("aq", "f")
                .setParameter("oq", "dd")
                .build();
        HttpGet httpGet = new HttpGet(uri);
        System.out.println(httpGet.getURI());

    }

复制代码

5.带参数的请求--post请求

复制代码

    @Test
    public void testHttpClientPost() throws IOException {
        //定义uri
        String uri="http://php.weather.sina.com.cn/iframe/index/w_cl.php";
        //需要传入的参数
        Map map = new HashMap();
        map.put("code", "js");
        map.put("day", "0");
        map.put("city", "上海");
        map.put("dfc", "1");
        map.put("charset", "utf-8");
        String encoding = "utf-8";
        //创建默认的httpclient
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建post请求对象
        HttpPost httpPost = new HttpPost(uri);
        //装填请求参数
        List list = new ArrayList();
        for (Map.Entry entry : map.entrySet()) {
                list.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
        }
        //设置参数到请求对象中
        httpPost.setEntity(new UrlEncodedFormEntity(list,encoding));

        System.out.println("请求地址:"+uri);
        System.out.println("请求参数:"+list.toString());

        //设置header信息
        //指定报文头【Content-type】、【User-Agent】
        httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
        httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

        //执行请求操作,并拿到结果(同步阻塞)
        CloseableHttpResponse response = httpClient.execute(httpPost);
        //获取所有的请求头信息
        Header[] allHeaders = response.getAllHeaders();
        for (Header allHeader : allHeaders) {
            System.out.println(allHeader.toString());
        }
        //获取结果实体
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            System.out.println(EntityUtils.toString(entity,encoding));
        }
     //关流
        EntityUtils.consume(entity);
        response.close();

    }

复制代码

你可能感兴趣的:(httpClient)