您的位置:首页 > 健康 > 养生 > HTTP 四、HttpClient的使用

HTTP 四、HttpClient的使用

2024/10/6 0:27:05 来源:https://blog.csdn.net/kkkkatoq/article/details/142106456  浏览:    关键词:HTTP 四、HttpClient的使用

一、简单介绍

        1、简介

        HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。

         HTTP和浏览器有点像,但却不是浏览器。很多人觉得既然HttpClient是一个HTTP客户端编程工具,很多人把他当做浏览器来理解,但是其实HttpClient不是浏览器,它是一个HTTP通信库,因此它只提供一个通用浏览器应用程序所期望的功能子集,最根本的区别是HttpClient中没有用户界面,浏览器需要一个渲染引擎来显示页面,并解释用户输入,例如鼠标点击显示页面上的某处,有一个布局引擎,计算如何显示HTML页面,包括级联样式表和图像。javascript解释器运行嵌入HTML页面或从HTML页面引用的javascript代码。来自用户界面的事件被传递到javascript解释器进行处理。除此之外,还有用于插件的接口,可以处理Applet,嵌入式媒体对象(如pdf文件,Quicktime电影和Flash动画)或ActiveX控件(可以执行任何操作)。HttpClient只能以编程的方式通过其API用于传输和接受HTTP消息。

      2、特性:
  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。
  18. 源代码基于Apache License 可免费获取。

二、简单使用

        1、引入依赖

        <dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.13</version></dependency>

        2、使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。

   //1. 创建HttpClient对象。CloseableHttpClient httpClient = HttpClientBuilder.create().build();//2. 创建请求方法的实例,并指定请求URL。// 如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。HttpGet httpGet = new HttpGet("http://localhost:8090/emp/get");//3. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HttpParams params)方法来添加请求参数;// 对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。// 响应模型CloseableHttpResponse response = null;try {// 4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,// 该方法返回一个HttpResponse。response = httpClient.execute(httpGet);// 从响应模型中获取响应实体//5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;// 调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。HttpEntity responseEntity = response.getEntity();System.out.println("响应状态为:" + response.getStatusLine());if (responseEntity != null) {System.out.println("响应内容长度为:" + responseEntity.getContentLength());System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));}} catch (ClientProtocolException e) {e.printStackTrace();} catch (ParseException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {// 6. 释放连接。无论执行方法是否成功,都必须释放连接if (httpClient != null) {httpClient.close();}if (response != null) {response.close();}} catch (IOException e) {e.printStackTrace();}}

三、各种使用案例

        1、GET请求  没有参数
       //1、创建HttpClient 客户端CloseableHttpClient httpClient = HttpClientBuilder.create().build();//2、 创建请求方法的实例,并指定请求URLHttpGet httpGet = new HttpGet("http://localhost:8090/user/get");//响应模型CloseableHttpResponse response = null;try {// 由客户端执行发送Get请求response = httpClient.execute(httpGet);// 从响应模型中获取响应实体HttpEntity responseEntity = response.getEntity();System.out.println("响应状态为:" + response.getStatusLine());if (responseEntity != null) {System.out.println("响应内容长度为:" + responseEntity.getContentLength());System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));}} catch (ClientProtocolException e) {e.printStackTrace();} catch (ParseException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {// 释放资源if (httpClient != null) {httpClient.close();}if (response != null) {response.close();}} catch (IOException e) {e.printStackTrace();}}
        2、GET 请求 路径上拼接参数

        1)直接拼接参数

String userId = "1";String userName = "张三";//1、创建HttpClient 客户端CloseableHttpClient httpClient = HttpClientBuilder.create().build();//参数 拼接StringBuffer params = new StringBuffer();try {// 字符数据最好encoding一下;这样一来,某些特殊字符才能传过去(如:某人的名字就是“&”,不encoding的话,传不过去)params.append("userId=" + URLEncoder.encode(userId, "utf-8"));params.append("&");params.append("userName="+ URLEncoder.encode(userName, "utf-8"));} catch (UnsupportedEncodingException e1) {e1.printStackTrace();}//2、 创建请求方法的实例,并指定请求URLHttpGet httpGet = new HttpGet("http://localhost:8090/user/get"+ "?" + params);//响应模型CloseableHttpResponse response = null;try {//添加配置信息RequestConfig requestConfig  = RequestConfig.custom()//设置连接超时时间(单位毫秒).setConnectTimeout(5000)//设置请求超时时间(单位毫秒).setConnectionRequestTimeout(5000)//socket读写超时时间(单位毫秒).setSocketTimeout(50000)//设置是否允许重定向(默认为true).setRedirectsEnabled(true).build();// 将上面的配置信息,添加到Get请求中httpGet.setConfig(requestConfig);// 由客户端执行发送Get请求response = httpClient.execute(httpGet);// 从响应模型中获取响应实体HttpEntity responseEntity = response.getEntity();System.out.println("响应状态为:" + response.getStatusLine());if (responseEntity != null) {System.out.println("响应内容长度为:" + responseEntity.getContentLength());System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));}} catch (ClientProtocolException e) {e.printStackTrace();} catch (ParseException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {// 释放资源if (httpClient != null) {httpClient.close();}if (response != null) {response.close();}} catch (IOException e) {e.printStackTrace();}}

        2)使用URI获得HttpGet

   String userId = "1";String userName = "张三";//1、创建HttpClient 客户端CloseableHttpClient httpClient = HttpClientBuilder.create().build();// 使用URI 拼接好请求路径URI uri = null;try {// 将参数放入键值对类NameValuePair中,再放入集合中List<NameValuePair> params = new ArrayList<>();params.add(new BasicNameValuePair("userId", userId));params.add(new BasicNameValuePair("userName", userName));// 设置uri信息,并将参数集合放入uri;// 注:这里也支持一个键值对一个键值对地往里面放setParameter(String key, String value)uri = new URIBuilder().setScheme("http").setHost("localhost").setPort(8090).setPath("/user/get").setParameters(params).build();} catch (URISyntaxException e1) {e1.printStackTrace();}//2、 创建请求方法的实例,并指定请求URLHttpGet httpGet = new HttpGet(uri);//响应模型CloseableHttpResponse response = null;try {//添加配置信息RequestConfig requestConfig  = RequestConfig.custom()//设置连接超时时间(单位毫秒).setConnectTimeout(5000)//设置请求超时时间(单位毫秒).setConnectionRequestTimeout(5000)//socket读写超时时间(单位毫秒).setSocketTimeout(50000)//设置是否允许重定向(默认为true).setRedirectsEnabled(true).build();// 将上面的配置信息,添加到Get请求中httpGet.setConfig(requestConfig);// 由客户端执行发送Get请求response = httpClient.execute(httpGet);// 从响应模型中获取响应实体HttpEntity responseEntity = response.getEntity();System.out.println("响应状态为:" + response.getStatusLine());if (responseEntity != null) {System.out.println("响应内容长度为:" + responseEntity.getContentLength());System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));}} catch (ClientProtocolException e) {e.printStackTrace();} catch (ParseException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {// 释放资源if (httpClient != null) {httpClient.close();}if (response != null) {response.close();}} catch (IOException e) {e.printStackTrace();}}
        3、POST请求 没有参数
         //1、创建HttpClient 客户端CloseableHttpClient httpClient = HttpClientBuilder.create().build();//2、 创建请求方法的实例,并指定请求URLHttpPost httpPost = new HttpPost("http://localhost:8090/user/get");//响应模型CloseableHttpResponse response = null;try {// 由客户端执行发送Post请求response = httpClient.execute(httpPost);// 从响应模型中获取响应实体HttpEntity responseEntity = response.getEntity();System.out.println("响应状态为:" + response.getStatusLine());if (responseEntity != null) {System.out.println("响应内容长度为:" + responseEntity.getContentLength());System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));}} catch (ClientProtocolException e) {e.printStackTrace();} catch (ParseException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {// 释放资源if (httpClient != null) {httpClient.close();}if (response != null) {response.close();}} catch (IOException e) {e.printStackTrace();}}

        4、POST请求 有参(对象参数 或 路径参数)

        1)路径拼接参数

        String userId = "1";String userName = "张三";//1、创建HttpClient 客户端CloseableHttpClient httpClient = HttpClientBuilder.create().build();//参数 拼接StringBuffer params = new StringBuffer();try {// 字符数据最好encoding一下;这样一来,某些特殊字符才能传过去(如:某人的名字就是“&”,不encoding的话,传不过去)params.append("userId=" + URLEncoder.encode(userId, "utf-8"));params.append("&");params.append("userName="+ URLEncoder.encode(userName, "utf-8"));} catch (UnsupportedEncodingException e1) {e1.printStackTrace();}//2、 创建请求方法的实例,并指定请求URLHttpPost httpPost = new HttpPost("http://localhost:8090/user/get"+"?"+params);// 设置ContentType(注:一般路径拼接参数请求头都是application/x-www-form-urlencoded)httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");//响应模型CloseableHttpResponse response = null;try {//添加配置信息RequestConfig requestConfig  = RequestConfig.custom()//设置连接超时时间(单位毫秒).setConnectTimeout(5000)//设置请求超时时间(单位毫秒).setConnectionRequestTimeout(5000)//socket读写超时时间(单位毫秒).setSocketTimeout(50000)//设置是否允许重定向(默认为true).setRedirectsEnabled(true).build();// 将上面的配置信息,添加到Get请求中httpPost.setConfig(requestConfig);// 由客户端执行发送Get请求response = httpClient.execute(httpPost);// 从响应模型中获取响应实体HttpEntity responseEntity = response.getEntity();System.out.println("响应状态为:" + response.getStatusLine());if (responseEntity != null) {System.out.println("响应内容长度为:" + responseEntity.getContentLength());System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));}} catch (ClientProtocolException e) {e.printStackTrace();} catch (ParseException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {// 释放资源if (httpClient != null) {httpClient.close();}if (response != null) {response.close();}} catch (IOException e) {e.printStackTrace();}}

        2)对象参数

//1、创建HttpClient 客户端CloseableHttpClient httpClient = HttpClientBuilder.create().build();//2、 创建请求方法的实例,并指定请求URLHttpPost httpPost = new HttpPost("http://localhost:8090/user/get");User user = new User();user.setId(1L);user.setName("张三");// 我这里利用阿里的fastjson,将Object转换为json字符串;// (需要导入com.alibaba.fastjson.JSON包)String jsonString = JSON.toJSONString(user);// post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中StringEntity entity = new StringEntity(jsonString, "UTF-8");httpPost.setEntity(entity);// json参数需要设置请求头为 application/jsonhttpPost.setHeader("Content-Type", "application/json;charset=utf8");//响应模型CloseableHttpResponse response = null;try {//添加配置信息RequestConfig requestConfig  = RequestConfig.custom()//设置连接超时时间(单位毫秒).setConnectTimeout(5000)//设置请求超时时间(单位毫秒).setConnectionRequestTimeout(5000)//socket读写超时时间(单位毫秒).setSocketTimeout(50000)//设置是否允许重定向(默认为true).setRedirectsEnabled(true).build();// 将上面的配置信息,添加到Get请求中httpPost.setConfig(requestConfig);// 由客户端执行发送Get请求response = httpClient.execute(httpPost);// 从响应模型中获取响应实体HttpEntity responseEntity = response.getEntity();System.out.println("响应状态为:" + response.getStatusLine());if (responseEntity != null) {System.out.println("响应内容长度为:" + responseEntity.getContentLength());System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));}} catch (ClientProtocolException e) {e.printStackTrace();} catch (ParseException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {// 释放资源if (httpClient != null) {httpClient.close();}if (response != null) {response.close();}} catch (IOException e) {e.printStackTrace();}}

        

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com