上篇文章给大家介绍了httpclient详细使用示例详解,喜欢的朋友可以点击查看,今天继续给大家介绍httpclient用法,具体内容如下所示;

1.简介

httpclient是apache jakarta common下的子项目,用来提供高效的、最新的、功能丰富的支持http协议的客户端编程工具包,并且它支持http协议最新的版本和建议。httpclient已经应用在很多的项目中,比如apache jakarta上很著名的另外两个开源项目cactus和htmlunit都使用了httpclient。
httpclient相比传统jdk自带的urlconnection,增加了易用性和灵活性,它不仅使客户端发送http请求变得容易,而且也方便开发人员测试接口(基于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 可免费获取。

3.使用方法

  1. 创建httpclient对象。
  2. 创建请求方法的实例,并指定请求url。如果需要发送get请求,创建httpget对象;如果需要发送post请求,创建httppost对象。
  3. 如果需要发送请求参数,可调用httpget、httppost共同的setparams(httpparams params)方法来添加请求参数;对于httppost对象而言,也可调用setentity(httpentity entity)方法来设置请求参数。
  4. 调用httpclient对象的execute(httpurirequest request)发送请求,该方法返回一个httpresponse。
  5. 调用httpresponse的getallheaders()、getheaders(string name)等方法可获取服务器的响应头;调用httpresponse的getentity()方法可获取httpentity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
  6. 释放连接。无论执行方法是否成功,都必须释放连接

4、实例

4.1 导入pom

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
     xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>

  <groupid>com.wo</groupid>
  <artifactid>httpclient_test</artifactid>
  <version>1.0-snapshot</version>

  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>2.0.3.release</version>
    <relativepath/> <!-- lookup parent from repository -->
  </parent>

  <dependencies>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
    <dependency>
      <groupid>org.apache.httpcomponents</groupid>
      <artifactid>httpclient</artifactid>
      <version>4.5.5</version>
    </dependency>
    <dependency>
      <groupid>com.alibaba</groupid>
      <artifactid>fastjson</artifactid>
      <version>1.2.47</version>
    </dependency>
  </dependencies>
</project>

4.2.get请求方式

@requestmapping("findall")
  public string findall() throws exception{
    //获得http客户端
    closeablehttpclient build = httpclientbuilder.create().build();
    //创建get请求
    httpget httpget = new httpget("http://localhost:8088/lunbo/findall");
    //执行请求
    closeablehttpresponse execute = build.execute(httpget);
    //解析返回值
    statusline statusline = execute.getstatusline();
    //获取到返回状态码
    system.out.println("状态码为:"+statusline.getstatuscode());
    string s = entityutils.tostring(execute.getentity());
    build.close();
    execute.close();
    return s;
  }

4.3 post请求方式

//post路径传参
  @requestmapping("/findallpost/{page}/{size}")
  public string findall(@pathvariable("page") int page,@pathvariable("size") int size) throws exception {
    //获得http客户端
    closeablehttpclient build = httpclientbuilder.create().build();
    //创建post请求
    httppost httppost = new httppost("http://localhost:8088/position/findall/"+page+"/"+size);
    //执行请求
    closeablehttpresponse execute = build.execute(httppost);
    //解析返回值
    statusline statusline = execute.getstatusline();
    //获取到返回状态码
    system.out.println("状态码为:"+statusline.getstatuscode());
    string s = entityutils.tostring(execute.getentity());
    build.close();
    execute.close();
    return s;
  }

  //post map传参
  @requestmapping("findbyid")
  public string findbyid(@requestparam("id") integer id)throws exception{
    //创建httpclicent请求对象
    closeablehttpclient build = httpclientbuilder.create().build();
    //声明请求方式
    httppost httppost = new httppost("http://localhost:8088/position/findbyid");
    //声明携带参数
    map map=new hashmap<>();
    map.put("id",id);
    //将map转换为json格式
    object o = jsonobject.tojson(map);
    //设置请求 参数的编码格式
    stringentity stringentity = new stringentity(o.tostring(), "utf-8");
    //将参数设置到请求对象中
    httppost.setentity(stringentity);
    //设置content-type
    httppost.setheader("content-type","application/json");
    //执行请求
    closeablehttpresponse execute = build.execute(httppost);
    //解析返回值
    statusline statusline = execute.getstatusline();
    //获取到返回状态码
    system.out.println("状态码为:"+statusline.getstatuscode());
    string s = entityutils.tostring(execute.getentity());
    build.close();
    execute.close();
    return s;
  }

到此这篇关于详解httpclient用法的文章就介绍到这了,更多相关httpclient用法内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!