参考:
https://blog.csdn.net/df0128/article/details/83043457
https://gitee.com/duanchaojie/java-code
https://blog.csdn.net/qq_34581161/article/details/117071185

一、概述

HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性,它不仅使客户端发送Http请求变得容易,而且也方便开发人员测试接口(基于Http协议的),提高了开发的效率,也方便提高代码的健壮性。

org.apache.commons.httpclient.HttpClient与org.apache.http.client.HttpClient的区别
Commons的HttpClient项目现在是生命的尽头,不再被开发, 已被Apache HttpComponents项目HttpClient和HttpCore 模组取代,提供更好的性能和更大的灵活性。

特性

  1. 基于标准、纯净的java语言。实现了Http1.0和Http1.1
    2. 以可扩展的面向对象的结构实现了Http全部的方法(GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE)。
    3. 支持HTTPS协议。
    4. 通过Http代理建立透明的连接。
    5. 利用CONNECT方法通过Http代理建立隧道的https连接。
    6. Basic, Digest, NTLMv1, NTLMv2, NTLM2 Session, SNPNEGO/Kerberos认证方案。
    7. 插件式的自定义认证方案。
    8. 便携可靠的套接字工厂使它更容易的使用第三方解决方案。
    9. 连接管理器支持多线程应用。支持设置最大连接数,同时支持设置每个主机的最大连接数,发现并关闭过期的连接。
    10. 自动处理Set-Cookie中的Cookie。
    11. 插件式的自定义Cookie策略。
    12. Request的输出流可以避免流中内容直接缓冲到socket服务器。
    13. Response的输入流可以有效的从socket服务器直接读取相应内容。
    14. 在http1.0和http1.1中利用KeepAlive保持持久连接。
    15. 直接获取服务器发送的response code和 headers。
    16. 设置连接超时的能力。
    17. 实验性的支持http1.1 response caching。.

    使用场景

  • 爬虫
  • 多系统之间接口交互

    快速上手

    引入jar ```xml org.apache.httpcomponents httpclient 4.5.13

org.apache.httpcomponents httpmime 4.5.13

  1. 下面是一个get请求的demo
  2. ```java
  3. public static void main(String[] args) {
  4. CloseableHttpClient httpClient = null;
  5. HttpGet httpGet = null;
  6. CloseableHttpResponse response = null;
  7. String result = "";
  8. try {
  9. //1、创建HttpClient对象
  10. httpClient = HttpClientBuilder.create().build();
  11. //2、创建HttpGet对象
  12. String url = "http://www.baidu.com";
  13. httpGet = new HttpGet(url);
  14. //3、执行get请求
  15. response = httpClient.execute(httpGet);
  16. //4、得到响应结果
  17. result = EntityUtils.toString(response.getEntity());
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }finally {
  21. try {
  22. if (response != null) {
  23. response.close();
  24. }
  25. if (httpRequestBase!= null){
  26. httpRequestBase.releaseConnection();
  27. }
  28. if (httpClient != null){
  29. httpClient.close();
  30. }
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }

二、实例和配置

原文链接:https://blog.csdn.net/df0128/article/details/83043457

httpClient 初始化

查看org.apache.http.impl.client.HttpClients.java源码可以看到httpClient 的创建有如下几种方式:
Factory methods for {@link CloseableHttpClient} instances.
image.png
最常用就是 createDefault() 方法和 custom() 方法;
下面分别对这几种方式做说明:

  1. //创建HttpClient对象的几种方式
  2. CloseableHttpClient httpClient = HttpClientBuilder.create().build();
  3. CloseableHttpClient httpClient = HttpClients.createDefault();
  4. CloseableHttpClient httpClient = HttpClients.createDefault();
  5. //此方法较为常用,为自定义的配置,可以附加各种配置
  6. CloseableHttpClient client = HttpClients.custom().
  7. setConnectionManager(new PoolingHttpClientConnectionManager()).build();

连接配置

自定义初始化HttpClient的时候需要设置连接管理,即上面代码的
.setConnectionManager(new PoolingHttpClientConnectionManager()) 部分,
此方法需要添加的实例为 HttpClientConnectionManager,这个接口有两个实现类,
PoolingHttpClientConnectionManagerBasicHttpClientConnectionManager

BasicHttpClientConnectionManager 是一个简单连接管理器,一次只保持一条连接,即便这个类是线程安全的,它也只应该被一个执行线程使用,BasicHttpClientConnectionManager对相同路由的连续请求将重用连接,如果新的连接跟已经持久化保持的连接不同,那么它会关闭已有的连接,根据所给的路由重新开启一个新的连接来使用,如果连接已经被分配出去了,那么java.lang.IllegalStateException异常会被抛出。
该连接管理器的实现应该在EJB容器内使用。一个例子如下:

  1. HttpClientContext context = HttpClientContext.create();
  2. HttpClientConnectionManager connMrg = new BasicHttpClientConnectionManager();
  3. HttpRoute route = new HttpRoute(new HttpHost("localhost", 80));
  4. // Request new connection. This can be a long process
  5. ConnectionRequest connRequest = connMrg.requestConnection(route, null);
  6. // Wait for connection up to 10 sec
  7. HttpClientConnection conn = connRequest.get(10, TimeUnit.SECONDS);
  8. try {
  9. // If not open
  10. if (!conn.isOpen()) {
  11. // establish connection based on its route info
  12. connMrg.connect(conn, route, 1000, context);
  13. // and mark it as route complete
  14. connMrg.routeComplete(conn, route, context);
  15. }
  16. // Do useful things with the connection.
  17. } finally {
  18. connMrg.releaseConnection(conn, null, 1, TimeUnit.MINUTES);
  19. }

PoolingHttpClientConnectionManager 是一个更加复杂的实现,其管理了一个连接池,能够为多个执行线程提供连接,连接依据路由归类放入到池中,当一个请求在连接池中有对应路由的连接时,连接管理器会从池中租借出一个持久化连接而不是创建一个带有标记的连接。
PoolingHttpClientConnectionManager 在总的和每条路由上都会保持最大数量限制的连接,默认该实现会为每个路由保持2个并行连接,总的数量上不超过20个连接,在现实使用中,这是限制可能太过于苛刻,尤其对于那些将HTTP作为传输协议的服务来说。
下面是一个如何调整连接池参数的例子:

  1. PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
  2. // Increase max total connection to 200
  3. cm.setMaxTotal(200);
  4. // Increase default max connection per route to 20
  5. cm.setDefaultMaxPerRoute(20);
  6. // Increase max connections for localhost:80 to 50
  7. HttpHost localhost = new HttpHost("locahost", 80);
  8. cm.setMaxPerRoute(new HttpRoute(localhost), 50);
  9. CloseableHttpClient httpClient = HttpClients.custom()
  10. .setConnectionManager(cm)
  11. .build();

请求配置

此部分配置其实可以附加在HttpClient上,也可以附加在请求的实例上,代码如下所示:

  1. RequestConfig.Builder configBuilder = RequestConfig.custom();
  2. // 设置连接超时
  3. configBuilder.setConnectTimeout(MAX_TIMEOUT);
  4. // 设置读取超时
  5. configBuilder.setSocketTimeout(MAX_TIMEOUT);
  6. // 设置从连接池获取连接实例的超时
  7. configBuilder.setConnectionRequestTimeout(MAX_TIMEOUT);
  8. // 在提交请求之前 测试连接是否可用
  9. configBuilder.setStaleConnectionCheckEnabled(true);
  10. //cookie管理规范设定,此处有多种可以设置,按需要设置
  11. configBuilder.setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY);
  12. RequestConfig requestConfig = configBuilder.build();
  13. CloseableHttpClient client = HttpClients.custom()
  14. .setConnectionManager(new PoolingHttpClientConnectionManager())
  15. .setDefaultRequestConfig(requestConfig).build();

上面代码中cookie策略的设定,CookieSpecs有多种实现,说明如下:
Standard strict(严格):状态管理策略行为完全符合RFC6265第四章的行为定义。
Standard(标准):状态管理策略较为符合RFC6265第四章定义的行为,以期望在不是完全遵守该行为之间的服务器之间进行交互。
Netscape 草案:该策略遵守Netscape公司公布最初的规范草案,除非确实需要与旧代码兼容,否则尽量避免使用它。
BEST_MATCH : 最佳匹配,不建议使用。
浏览器兼容性Browser compatibility(已过时):该策略尝试尽量去模拟老旧的浏览器版本如微软IE和Mozilla FireFox,请不要在新应用中使用。
Default:默认cookie策略是一种综合性的策略,其基于HTTP response返回的cookie属性如version信息,过期信息,与RFC2965,RFC2109或者Netscape草案兼容,该策略将会在下一个HttpClient小版本(基于RFC6265)中废弃。
Ignore cookies:所有的cookie都被忽略
强烈建议在新应用中使用Standard或者Standard strict策略,过时规范应该仅仅是在与旧系统兼容时使用。下一个HttpClient版本将会停止对过时规范的支持。

定制cookie策略

如果要定制cookie策略,则需要创建一个自定义的CookieSpec接口的实现,创建一个CookieSpecProvider的实现类,然后用该实现类去创建和初始化自定义规范的实例,然后使用HttpClient进行注册,一旦自定义规范被注册,它就会如同标准cookie规范一样被触发。
如下为一个实现自定义cookie策略的范例:

  1. PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.getDefault();
  2. Registry<CookieSpecProvider> r = RegistryBuilder.<CookieSpecProvider>create()
  3. .register(CookieSpecs.DEFAULT,
  4. new DefaultCookieSpecProvider(publicSuffixMatcher))
  5. .register(CookieSpecs.STANDARD,
  6. new RFC6265CookieSpecProvider(publicSuffixMatcher))
  7. .register("easy", new EasySpecProvider())
  8. .build();
  9. RequestConfig requestConfig = RequestConfig.custom()
  10. .setCookieSpec("easy")
  11. .build();
  12. CloseableHttpClient httpclient = HttpClients.custom()
  13. .setDefaultCookieSpecRegistry(r)
  14. .setDefaultRequestConfig(requestConfig)
  15. .build();

cookie持久化

有时候有些cookie内容我们并不想每次执行就发送一次,比如登录等,那么我们可以直接把定制好的cookie附加到HttpClient上,这样就可以避免重复动作了,代码如下所示:

  1. // Create a local instance of cookie store
  2. CookieStore cookieStore = new BasicCookieStore();
  3. // Populate cookies if needed
  4. BasicClientCookie cookie = new BasicClientCookie("name", "value");
  5. cookie.setDomain(".mycompany.com");
  6. cookie.setPath("/");
  7. cookieStore.addCookie(cookie);
  8. CloseableHttpClient httpclient = HttpClients.custom()
  9. .setDefaultCookieStore(cookieStore)
  10. .build();

三、请求和响应

HttpClient支持开箱即用的,所有定义在HTTP/1.1规范中的方法:
GET、POST、HEAD、PUT、DELETE、TRACE、OPTIONS。
每个方法类型都有一个指定的类:HttpGet、HttpPost、HttpHead、HttpPut、HttpDelete、HttpTrace、HttpOptions。
譬如一个get请求如下:

  1. HttpGet httpget = new HttpGet(
  2. "http://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=");

参数为请求的地址,? 号后边的部分是参数;当然也可以用URIBuilder 构建一个URI的完整路径,譬如上面例子中的请求用URI方式的话代码如下:

  1. URI uri = new URIBuilder()
  2. .setScheme("http")
  3. .setHost("www.google.com")
  4. .setPath("/search")
  5. .setParameter("q", "httpclient")
  6. .setParameter("btnG", "Google Search")
  7. .setParameter("aq", "f")
  8. .setParameter("oq", "")
  9. .build();
  10. HttpGet httpget = new HttpGet(uri);
  11. System.out.println(httpget.getURI());

这种参数为URI的并不常用,不建议使用;

get请求(无参)

客户端:

  1. @Test
  2. public void doGetTestOne() {
  3. // 1、获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
  4. CloseableHttpClient httpClient = HttpClientBuilder.create().build();
  5. // 2、创建Get请求
  6. HttpGet httpGet = new HttpGet("https://www.baidu.com/");
  7. // 响应模型
  8. CloseableHttpResponse response = null;
  9. try {
  10. // 3、由客户端执行(发送)Get请求
  11. response = httpClient.execute(httpGet);
  12. //4、加入请求头
  13. httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36");
  14. httpGet.setHeader("Referer","https://www.baidu.com/");
  15. /**
  16. * 5、解析响应获取数据
  17. * 判断状态码是否是 200
  18. */
  19. if (response.getStatusLine().getStatusCode() == 200) {
  20. HttpEntity entity = response.getEntity();
  21. //将响应对象转化为字符串
  22. String content = EntityUtils.toString(entity, "utf-8");
  23. System.out.println(content);
  24. }
  25. } catch (ClientProtocolException e) {
  26. e.printStackTrace();
  27. } catch (ParseException e) {
  28. e.printStackTrace();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. } finally {
  32. try {
  33. // 释放资源
  34. if (httpClient != null) {
  35. httpClient.close();
  36. }
  37. if (response != null) {
  38. response.close();
  39. }
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. }

响应内容:
可以看到请求的结果为百度源代码:

  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>

get请求(有参)

1、直接使用Java字符串拼接(推荐)

2、uriBuilder 构建get请求参数

  1. public static void main(String[] args) throws Exception {
  2. // 1.HttpClients工具类创建HttpClient对象
  3. CloseableHttpClient httpClient = HttpClients.createDefault();
  4. // 设置请求地址是: http://yun.itheima.com/search?keys=Java
  5. // 使用uriBuilder 构建get请求参数
  6. URIBuilder uriBuilder = new URIBuilder("http://yun.itheima.com/search");
  7. // 多个参数,使用连式编程
  8. uriBuilder.setParameter("keys","Java");
  9. // 2.创建HttpGet对象, 设置url访问地址☆
  10. HttpGet httpGet = new HttpGet(uriBuilder.build());
  11. System.out.println("发送请求的信息:");
  12. // 3.使用 HttpClient 发起请求, 获取 response
  13. CloseableHttpResponse response = httpClient.execute(httpGet);
  14. // 4.解析响应
  15. if (response.getStatusLine().getStatusCode() == 200){
  16. String content = EntityUtils.toString(response.getEntity(), "utf-8");
  17. System.out.println(content.length());
  18. System.out.println(content);
  19. }
  20. }

3、UrlEncodedFormEntity + List 构建get请求参数

UrlEncodedFormEntity 将List 列表中k-v转化为浏览器get请求格式的字符串

  1. @Test
  2. void sendGet(){
  3. CloseableHttpClient client = null;
  4. HttpGet httpGet = null;
  5. CloseableHttpResponse response = null;
  6. try {
  7. client = HttpClientBuilder.create().build();
  8. // 构建参数列表【BasicNameValuePair】
  9. List<BasicNameValuePair> pairs = new ArrayList<>();
  10. pairs.add(new BasicNameValuePair("page","1"));
  11. pairs.add(new BasicNameValuePair("limit","5"));
  12. // 设置参数的编码
  13. UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(pairs,"utf-8");
  14. String params = EntityUtils.toString(urlEncodedFormEntity, "utf-8");
  15. System.out.println(params); // 将参数转成page=1&limit=5格式
  16. httpGet = new HttpGet("http://localhost:8080/user/queryUserList?"+params);
  17. // 发送请求
  18. response = client.execute(httpGet);
  19. String result = EntityUtils.toString(response.getEntity(), "utf-8");
  20. System.out.println(result);
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }finally {
  24. try {
  25. if (response != null) {
  26. response.close();
  27. }
  28. if (httpGet != null){
  29. httpGet.releaseConnection();
  30. }
  31. if (client != null){
  32. client.close();
  33. }
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }

服务端:

  1. @GetMapping("queryUserList")
  2. public PageBean queryUserList(UserVO vo){
  3. return userService.queryUserList(vo);
  4. }

post请求

image.png
从网上下载一个png图片时,响应到达服务器如tomcat 后 获取文件后缀名在 tomcat服务器的 conf/web.xml 文件中找到对应的mime-mapping,设置响应头 Content-Type 为 mime-type

form表单enctype 可用的MIME类型有(Content-Type类型):

  • application/x-www-form-urlencode (默认)
  • multipart/form-data
  • application/json
  • text/plain

示例1:application/x-www-form-urlencode
image.png
示例2:post发送json类型的数据
image.png
示例2:文件
服务端:
image.png
客户端:

  1. @Test
  2. void sendPost(){
  3. CloseableHttpClient httpClient = null;
  4. HttpPost httpPost = null;
  5. CloseableHttpResponse response = null;
  6. try {
  7. httpClient = HttpClientBuilder.create().build();
  8. httpPost = new HttpPost("http://localhost:8080/user/addUser");
  9. // 构建参数列表【BasicNameValuePair】
  10. List<BasicNameValuePair> pairs = new ArrayList<>();
  11. pairs.add(new BasicNameValuePair("name","张三"));
  12. pairs.add(new BasicNameValuePair("sex","1"));
  13. // 设置参数编码
  14. UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(pairs,"utf-8");
  15. // 发送请求之前给httpPost设置参数
  16. httpPost.setEntity(urlEncodedFormEntity);
  17. response = httpClient.execute(httpPost);
  18. String result = EntityUtils.toString(response.getEntity(), "utf-8");
  19. System.out.println(result);
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }finally {
  23. try {
  24. if (response != null) {
  25. response.close();
  26. }
  27. if (httpPost != null){
  28. httpPost.releaseConnection();
  29. }
  30. if (httpClient != null){
  31. httpClient.close();
  32. }
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. }

服务端:

  1. @PostMapping("addUser")
  2. public ResultObj addUser(User user){
  3. try {
  4. String salt = IdUtil.simpleUUID().toUpperCase();
  5. user.setSalt(salt);
  6. user.setPwd(new Md5Hash(Constant.USER_PASSWORD,salt,2).toString());
  7. userService.addUser(user);
  8. return ResultObj.ADD_SUCCESS;
  9. } catch (Exception e) {
  10. e.printStackTrace();
  11. return ResultObj.ADD_ERROR;
  12. }
  13. }

post请求(json)

  1. // params传入的对象
  2. public static String sendPostJson(String url, Map<String,String> params){
  3. CloseableHttpClient httpClient = null;
  4. HttpPost httpPost = null;
  5. CloseableHttpResponse response = null;
  6. String result = "";
  7. try {
  8. httpClient = HttpClientBuilder.create().build();
  9. httpPost = new HttpPost(url);
  10. // 设置参数
  11. if (null != params && params.size() > 0){
  12. String paramJson = JSONObject.toJSONString(params);
  13. StringEntity stringEntity = new StringEntity(paramJson,"utf-8");
  14. stringEntity.setContentType("application/json;charset=utf-8");
  15. httpPost.setEntity(stringEntity);
  16. }
  17. response = httpClient.execute(httpPost);
  18. result = EntityUtils.toString(response.getEntity());
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. }finally {
  22. close(response,httpPost,httpClient);
  23. }
  24. return result;
  25. }

服务端:

  1. @PostMapping("addUser")
  2. public ResultObj addUser(@RequestBody User user){
  3. try {
  4. String salt = IdUtil.simpleUUID().toUpperCase();
  5. user.setSalt(salt);
  6. user.setPwd(new Md5Hash(Constant.USER_PASSWORD,salt,2).toString());
  7. userService.addUser(user);
  8. return ResultObj.ADD_SUCCESS;
  9. } catch (Exception e) {
  10. e.printStackTrace();
  11. return ResultObj.ADD_ERROR;
  12. }
  13. }

post请求(文件)

请求添加cookie

这里cookie的添加只能是通过HttpClient实例添加或者在执行的时候添加;
方法一、通过HttpClient实例添加:

  1. BasicCookieStore cookieStore = new BasicCookieStore();
  2. BasicClientCookie cookie = new BasicClientCookie("aaa", "bbb");
  3. cookie.setDomain(".mycompany.com");
  4. cookie.setPath("/");
  5. cookieStore.addCookie(cookie);
  6. CloseableHttpClient client = HttpClients.custom()
  7. .setDefaultCookieStore(cookieStore).build();

方法二、通过HttpContext添加,代码如下:

  1. //实例化一个cookieStore并添加cookie
  2. BasicCookieStore cookieStore = new BasicCookieStore();
  3. BasicClientCookie cookie = new BasicClientCookie("aaa", "bbb");
  4. cookie.setDomain(".mycompany.com");
  5. cookie.setPath("/");
  6. cookieStore.addCookie(cookie);
  7. CloseableHttpClient client = HttpClients.createDefault();
  8. HttpGet get = new HttpGet("http://www.baidu.com");
  9. HttpClientContext context = new HttpClientContext(); //实例化一个HttpContext
  10. context.setCookieStore(cookieStore); //添加cookieStore
  11. CloseableHttpResponse response = null;
  12. try {
  13. response = client.execute(get, context);
  14. System.out.println(response.getStatusLine().getStatusCode());
  15. } catch (ClientProtocolException e) {
  16. e.printStackTrace();
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. }finally
  20. {
  21. try {
  22. response.close();
  23. client.close();
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }

请求体

  1. CloseableHttpClient httpClient = HttpClientBuilder.create().build();
  2. HttpGet httpGet = new HttpGet("https://www.baidu.com/");
  3. //1、加入请求头
  4. httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36");
  5. httpGet.setHeader("Referer","https://www.baidu.com/");
  6. //请求参数之间以 & 连接 如 https://www.baidu.com/s?ie=utf-8&f=8&wd=htttpclient
  7. // Map<String,String> params 参数是Map
  8. List<BasicNameValuePair> pairList = new ArrayList<>();
  9. params.forEach((x,y) -> pairList.add(new BasicNameValuePair(x,y)));
  10. UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(pairList,"utf-8");
  11. // 将参数转成page=1&limit=5格式
  12. String param = EntityUtils.toString(urlEncodedFormEntity, "utf-8");
  13. httpGet = new HttpGet(url+"?"+param);
  14. CloseableHttpResponse response = httpClient.execute(httpGet);

HttpGet 请求对象的一些常用方法

  1. //获取请求方法
  2. public abstract String getMethod();
  3. //请求URI
  4. public URI getURI()
  5. public void setURI()
  6. //请求行
  7. public RequestLine getRequestLine() {
  8. final String method = getMethod();
  9. final ProtocolVersion ver = getProtocolVersion();
  10. final URI uriCopy = getURI(); // avoids possible window where URI could be changed
  11. String uritext = null;
  12. if (uriCopy != null) {
  13. uritext = uriCopy.toASCIIString();
  14. }
  15. if (uritext == null || uritext.isEmpty()) {
  16. uritext = "/";
  17. }
  18. return new BasicRequestLine(method, uritext, ver);
  19. }
  20. //请求配置
  21. public void setConfig(final RequestConfig config)

示例1:配置请求代理
image.png
示例2:连接超时和响应超时
image.png

响应体

响应即是执行请求的返回,为 CloseableHttpResponse ,这个对象中含有我们所需要的所有的返回,譬如 header,cookie和 响应内容等,其基本内容如下代码所示:

  1. CloseableHttpClient httpClient = HttpClientBuilder.create().build();
  2. HttpGet httpGet = new HttpGet("https://www.baidu.com/");
  3. CloseableHttpResponse response = httpClient.execute(httpGet);
  4. //获取响应状态
  5. //将响应体转化为字符串
  6. response = httpClient.execute(httpGet);
  7. if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
  8. HttpEntity entity = response.getEntity();
  9. result = EntityUtils.toString(entity,"utf-8");
  10. }
  11. - public HttpEntity getEntity() 获取请求体
  12. - public StatusLine getStatusLine() 获取请求状态
  13. - public HttpParams getParams() 获取参数
  14. - public Header[] getAllHeaders() 获取所有请求头

再来看一个示例:

  1. public static void main(String[] args) {
  2. BasicCookieStore cookieStore = new BasicCookieStore();
  3. BasicClientCookie cookie = new BasicClientCookie("aaa", "bbb");
  4. cookie.setDomain(".mycompany.com");
  5. cookie.setPath("/");
  6. cookieStore.addCookie(cookie);
  7. CloseableHttpClient client = HttpClients.createDefault();
  8. HttpGet get = new HttpGet("http://www.baidu.com");
  9. HttpClientContext context = new HttpClientContext();
  10. context.setCookieStore(cookieStore);
  11. CloseableHttpResponse response = null;
  12. try {
  13. response = client.execute(get, context);
  14. System.out.println(response.getStatusLine().getStatusCode());//返回状态值
  15. Header[] headers = response.getAllHeaders();//获取所有的header信息
  16. boolean isContains = response.containsHeader("name");//是否包含ke为name的header
  17. ProtocolVersion version = response.getProtocolVersion();//获取协议版本
  18. // 使用响应对象获取响应实体
  19. HttpEntity entity = httpResponse.getEntity();
  20. //将响应实体转为字符串,此部分即为内容
  21. try {
  22. String response = EntityUtils.toString(entity,"utf-8");
  23. } catch (ParseException e) {
  24. log.error("获取响应内容转码失败,转码类型为:{}", "utf-8", e);
  25. } catch (IOException e) {
  26. log.error("获取响应内容转码失败,转码类型为:{}", "utf-8", e);
  27. }
  28. //获取cookie
  29. List<Cookie> returnCookie = context.getCookieStore().getCookies();
  30. } catch (ClientProtocolException e) {
  31. e.printStackTrace();
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }finally
  35. {
  36. try {
  37. response.close();
  38. client.close();
  39. } catch (IOException e) {
  40. // TODO Auto-generated catch block
  41. e.printStackTrace();
  42. }
  43. }
  44. }

EntityUtils.java 响应体工具类的几个重要方法

  1. //确保请求体被完全消费(使用),如果还存在就关闭
  2. public static void consume(final HttpEntity entity)
  3. //返回请求体的二进制数组
  4. public static byte[] toByteArray(final HttpEntity entity) throws IOException
  5. //获取请求体内容的字符串表示 默认编码是 ISO-8859-1
  6. public static String toString(final HttpEntity entity, final Charset defaultCharset)

发送https请求

有部分网站为https开头,这样的话就需要特别一点的操作了,如下范例为一个发送https请求的例子:

  1. /**
  2. * 发送 SSL POST请(HTTPS),K-V形式
  3. * @param url
  4. * @param params
  5. * @author Charlie.chen
  6. */
  7. public static CloseableHttpResponse httpPostWithSSL(String url, Map<String, Object> params)
  8. {
  9. CloseableHttpClient httpClient = HttpClients.custom()
  10. .setSSLSocketFactory(createSSLConn()).setConnectionManager(connMgr)
  11. .setDefaultRequestConfig(requestConfig).build();
  12. HttpPost httpPost = new HttpPost(url);
  13. CloseableHttpResponse response = null;
  14. try {
  15. httpPost.setConfig(requestConfig);
  16. if(params != null && !params.isEmpty())
  17. {
  18. List<NameValuePair> pairList = new ArrayList<NameValuePair>(params.size());
  19. for (Map.Entry<String, Object> entry : params.entrySet()) {
  20. NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry
  21. .getValue().toString());
  22. pairList.add(pair);
  23. }
  24. httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName("utf-8")));
  25. }
  26. response = httpClient.execute(httpPost);
  27. } catch (Exception e)
  28. {
  29. log.error("下发接口失败", e);
  30. return null;
  31. }
  32. return response;
  33. }
  34. /**
  35. * 创建SSL安全连接
  36. * @param url
  37. * @param params
  38. * @return
  39. */
  40. private static SSLConnectionSocketFactory createSSLConn() {
  41. SSLConnectionSocketFactory sslsf = null;
  42. try
  43. {
  44. SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
  45. public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  46. return true;
  47. }
  48. }).build();
  49. sslsf = new SSLConnectionSocketFactory(sslContext);
  50. } catch (GeneralSecurityException e)
  51. {
  52. e.printStackTrace();
  53. }
  54. return sslsf;
  55. }

四、实操演练

1)二进制文件下载

其实就是对响应对象进行处理:

  1. public static boolean downLoadFile(String url, String targetFilePath) {
  2. CloseableHttpClient httpclient = getHttpClient();
  3. HttpPost post = new HttpPost(url);
  4. CloseableHttpResponse httpResponse = null;
  5. try {
  6. httpResponse = httpclient.execute(post);
  7. FileOutputStream output = new FileOutputStream(targetFilePath);
  8. // 得到网络资源的字节数组,并写入文件
  9. HttpEntity entity = httpResponse.getEntity();
  10. if (entity != null) {
  11. InputStream instream = entity.getContent();
  12. byte b[] = new byte[1024];
  13. int j = 0;
  14. while( (j = instream.read(b))!=-1){
  15. output.write(b,0,j);
  16. }
  17. output.flush();
  18. output.close();
  19. instream.close();
  20. }
  21. } catch (ClientProtocolException e) {
  22. e.printStackTrace();
  23. return false;
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. return false;
  27. }finally
  28. {
  29. try {
  30. httpResponse.close();
  31. httpclient.close();
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. return true;
  37. }

2)文件上传和Json数据获取

需求:通过SM.MS图床提供的token API,完成向图床上传图片 、删除图片和获取上传历史 三个功能
https://sm.ms/home/picture
下图是上传到图床的图片数据(需要先登录用户)
image.png
完整代码:
springboot项目的pom依赖:

  1. <dependency>
  2. <groupId>org.apache.httpcomponents</groupId>
  3. <artifactId>httpclient</artifactId>
  4. <version>4.5.13</version>
  5. </dependency>
  6. <!--上传文件时用到-->
  7. <dependency>
  8. <groupId>org.apache.httpcomponents</groupId>
  9. <artifactId>httpmime</artifactId>
  10. <version>4.5.13</version>
  11. </dependency>

Controller层:

  1. @Controller
  2. @RequestMapping("/smms")
  3. public class ApiRequestController {
  4. //创建连接池管理器
  5. PoolingHttpClientConnectionManager cm = null;
  6. CloseableHttpClient httpClient = null;
  7. {
  8. //创建连接池管理器
  9. cm = new PoolingHttpClientConnectionManager();
  10. //设置最大的连接数
  11. cm.setMaxTotal(200);
  12. //设置每个主机的最大连接数,访问每一个网站指定的连接数,不会影响其他网站的访问
  13. cm.setDefaultMaxPerRoute(20);
  14. //使用连接池管理器获取连接并发起请求
  15. httpClient = HttpClients.custom().setConnectionManager(cm).build();
  16. }
  17. @PostMapping("/upload")
  18. @ResponseBody
  19. public String uploadImg(@RequestParam("smfile") List<MultipartFile> multipartFiles) throws IOException {
  20. //CloseableHttpClient httpClient = HttpClientBuilder.create().build();
  21. HttpPost httpPost = new HttpPost("https://sm.ms/api/v2/upload");
  22. CloseableHttpResponse response = null;
  23. String responseStr = "";
  24. try {
  25. httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36");
  26. httpPost.setHeader("referer","https://sm.ms/");
  27. httpPost.setHeader("Authorization","CWQjdeMixpUUeSeu4f0tg98OeaTZuToD");
  28. MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
  29. for (MultipartFile multipartFile : multipartFiles) {
  30. multipartEntityBuilder.addBinaryBody("smfile", multipartFile.getInputStream(), ContentType.MULTIPART_FORM_DATA, URLEncoder.encode(multipartFile.getOriginalFilename(), StandardCharsets.UTF_8));
  31. }
  32. HttpEntity httpEntity = multipartEntityBuilder.build();
  33. httpPost.setEntity(httpEntity);
  34. response = httpClient.execute(httpPost);
  35. HttpEntity responseEntity = response.getEntity();
  36. responseStr = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
  37. System.out.println(responseStr);
  38. } catch (ParseException | IOException e) {
  39. e.printStackTrace();
  40. } finally {
  41. HttpClientUtils.closeQuietly(response);
  42. }
  43. return responseStr;
  44. }
  45. @GetMapping("/upload_history")
  46. @ResponseBody
  47. public Object getImgUploadHistory(){
  48. System.out.println("httpClient----------->");
  49. System.out.println(httpClient);
  50. //CloseableHttpClient httpClient = HttpClientBuilder.create().build();;
  51. HttpGet httpGet = new HttpGet("https://sm.ms/api/v2/upload_history");
  52. CloseableHttpResponse response = null;
  53. String ret = "";
  54. try {
  55. httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36");
  56. httpGet.setHeader("referer","https://sm.ms/");
  57. httpGet.setHeader("Authorization","CWQjdeMixpUUeSeu4f0tg98OeaTZuToD");
  58. httpGet.setHeader("Content-Type","multipart/form-data");
  59. // 设置配置请求参数
  60. RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间
  61. .setConnectionRequestTimeout(35000)// 请求超时时间
  62. .setSocketTimeout(60000)// 数据读取超时时间
  63. .build();
  64. // 为httpGet实例设置配置
  65. httpGet.setConfig(requestConfig);
  66. response = httpClient.execute(httpGet);
  67. ret = EntityUtils.toString(response.getEntity(),"utf-8");
  68. System.out.println(ret);
  69. System.out.println(JSONObject.parse(ret));
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. }finally {
  73. HttpClientUtils.closeQuietly(response);
  74. }
  75. return ret;
  76. }
  77. @GetMapping("/delete")
  78. @ResponseBody
  79. public Object deleteImage(@RequestParam("imghash") String imghash){
  80. //CloseableHttpClient httpClient = HttpClientBuilder.create().build();;
  81. HttpGet httpGet = new HttpGet("https://sm.ms/api/v2/delete/" + imghash);
  82. CloseableHttpResponse response = null;
  83. String ret = "";
  84. try {
  85. httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36");
  86. httpGet.setHeader("referer","https://sm.ms/");
  87. httpGet.setHeader("Authorization","CWQjdeMixpUUeSeu4f0tg98OeaTZuToD");
  88. response = httpClient.execute(httpGet);
  89. ret = EntityUtils.toString(response.getEntity(),"utf-8");
  90. System.out.println(ret);
  91. } catch (IOException e) {
  92. e.printStackTrace();
  93. }finally {
  94. HttpClientUtils.closeQuietly(response);
  95. }
  96. return ret;
  97. }
  98. }

前端页面效果:
image.png
前端代码:

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <!-- 引入vue -->
  7. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  8. <!-- 引入element-ui -->
  9. <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  10. <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  11. <!-- 引入axios -->
  12. <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  13. <style>
  14. #app{
  15. margin: 0 auto;
  16. width: 80%;
  17. border: 1px solid red;
  18. padding: 15px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="app">
  24. <h2>上传图片到SM.MS图床</h2>
  25. <el-upload
  26. class="upload-demo"
  27. action="/smms/upload"
  28. multiple
  29. drag
  30. name="smfile"
  31. :on-success="handleImagesSuccess">
  32. <i class="el-icon-upload"></i>
  33. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  34. </el-upload>
  35. <h2>SM.MS图床的上传历史记录</h2>
  36. <el-table
  37. :data="imagesTableData"
  38. border
  39. stripe
  40. style="width: 100%">
  41. <el-table-column
  42. label="Hash"
  43. width="150">
  44. <template slot-scope="scope">
  45. <span style="margin-left: 10px">{{ scope.row.hash }}</span>
  46. </template>
  47. </el-table-column>
  48. <el-table-column
  49. label="文件名"
  50. width="180">
  51. <template slot-scope="scope">
  52. <span style="margin-left: 10px">{{ scope.row.filename }}</span>
  53. </template>
  54. </el-table-column>
  55. <el-table-column
  56. label="预览"
  57. width="180">
  58. <template slot-scope="scope">
  59. <el-image
  60. style="width: 150px; height: auto"
  61. :src="scope.row.url"
  62. fit="fill"></el-image>
  63. </template>
  64. </el-table-column>
  65. <el-table-column
  66. label="Size"
  67. width="100">
  68. <template slot-scope="scope">
  69. <span style="margin-left: 10px">{{ scope.row.size }}</span>
  70. </template>
  71. </el-table-column>
  72. <el-table-column
  73. label="图片尺寸"
  74. width="120">
  75. <template slot-scope="scope">
  76. <span style="margin-left: 10px">{{ scope.row.width}} / {{scope.row.height}}</span>
  77. </template>
  78. </el-table-column>
  79. <el-table-column label="操作">
  80. <template slot-scope="scope">
  81. <el-button
  82. size="mini"
  83. @click="handleCopyUrl(scope.$index, scope.row)">获取URL</el-button>
  84. <el-button
  85. size="mini"
  86. type="danger"
  87. @click="handleDelete(scope.$index, scope.row)">删除</el-button>
  88. </template>
  89. </el-table-column>
  90. </el-table>
  91. </div>
  92. <script>
  93. const app = new Vue({
  94. el: '#app',
  95. data:{
  96. imagesTableData:[]
  97. },
  98. methods:{
  99. //处理:图片上传成功
  100. handleImagesSuccess(res, file){
  101. if(res.success == true){
  102. this.imagesTableData.push(res.data);
  103. this.$message({
  104. message: "图片:" + row.data.filename + " 上传成功",
  105. type:"success"
  106. });
  107. }
  108. },
  109. handleCopyUrl(index, row){
  110. this.$message({
  111. message:'图片的URL地址为:'+row.url,
  112. type:"success"
  113. });
  114. },
  115. handleDelete(index, row){
  116. console.log(index);
  117. axios.get("/smms/delete?imghash=" + row.hash).then(res =>{
  118. console.log(res);
  119. if(res.status === 200){
  120. this.imagesTableData.splice(index,1);
  121. this.$message({
  122. message: '图片删除成功',
  123. type: 'success'
  124. });
  125. }
  126. })
  127. },
  128. //请求上传历史
  129. _getHistoryUpload() {
  130. console.log("获取历史上传记录...");
  131. axios({
  132. "method": 'get',
  133. "url": "/smms/upload_history",
  134. }).then(res =>{
  135. console.log(res);
  136. if(res.status === 200){
  137. this.imagesTableData.splice(0);
  138. this.imagesTableData.push(...res.data.data);
  139. }
  140. })
  141. }
  142. },
  143. mounted(){
  144. },
  145. created(){
  146. this.$nextTick(() =>{
  147. this._getHistoryUpload();
  148. })
  149. }
  150. })
  151. </script>
  152. </body>
  153. </html>