zoukankan      html  css  js  c++  java
  • 接口httpClient 以及HttpClient与CloseableHttpClient之间的区别

    最近在工作的过程中有遇到httpClient接口,今天特意些一个小示例对这个知识点进行温习。

    下面是代码小片段:

     1 package com.sinosoft.lis.mgubq.zhaoyongqiang;
     2 
     3 import org.apache.http.HttpEntity;
     4 import org.apache.http.client.ClientProtocolException;
     5 import org.apache.http.client.methods.CloseableHttpResponse;
     6 import org.apache.http.client.methods.HttpGet;
     7 import org.apache.http.impl.client.CloseableHttpClient;
     8 import org.apache.http.impl.client.HttpClients;
     9 import org.apache.http.util.EntityUtils;
    10 
    11 import java.io.IOException;
    12 
    13 /**
    14  * Created by zyq on 2019/9/11.
    15  */
    16 public class HttpClientTest {
    17     //练习使用HttpClient接口的使用
    18     public static void main(String[] args) {
    19         // 创建httpClient实例
    20         CloseableHttpClient httpClient = HttpClients.createDefault();
    21         // 创建httpGet实例
    22         HttpGet httpGet = new HttpGet("https://www.baidu.com");  // http://www.tuicool.com/
    23         CloseableHttpResponse response;
    24         try {
    25             response = httpClient.execute(httpGet);
    26             if(response != null){
    27                 HttpEntity entity = response.getEntity();   // 获取网页内容
    28                 String result = EntityUtils.toString(entity, "UTF-8");
    29                 System.out.println("网页内容:" + result);
    30             }
    31             if(response != null){
    32                 response.close();
    33             }
    34             if(httpClient != null){
    35                 httpClient.close();
    36             }
    37         } catch (ClientProtocolException e) {
    38             // TODO Auto-generated catch block
    39             e.printStackTrace();
    40         } catch (IOException e) {
    41             // TODO Auto-generated catch block
    42             e.printStackTrace();
    43         }
    44 
    45     }
    46 }

    最终的运行结果:(这个应该就是我们非常熟悉的百度的首页的html代码了)

    1 网页内容:<!DOCTYPE html>
    2 <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');
    3                 </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

    HttpClient与CloseableHttpClient

    https://www.jianshu.com/p/82f30208ae56

  • 相关阅读:
    改变多行文本字符串的缩进
    多线程
    python基本语法2.5--字符串的相关操作
    python基本语法2.4---汉诺塔的递归
    python基本语法2.3--函数及参数传递
    python基本语法2.2--函数名当作变量传递
    python基本语法2.1--if判断和while,for循环
    AlexNet源码
    python基本语法1.4--初识爬虫
    python基本语法1.5--调用numpy库的性能影响
  • 原文地址:https://www.cnblogs.com/dongyaotou/p/11509928.html
Copyright © 2011-2022 走看看