httpclient

特性

1. 基于标准、纯净的Java语言。实现了Http1.0Http1.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.0http1.1中利用KeepAlive保持持久连接。

15. 直接获取服务器发送的response code headers

16. 设置连接超时的能力。

17. 实验性的支持http1.1 response caching

18. 源代码基于Apache License 可免费获取。

-------------------------------------------------------------------------------------------

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

1. 创建HttpClient对象。

2. 创建请求方法的实例,并指定请求URL

    如果需要发送GET请求,创建HttpGet对象;

    如果需要发送POST请求,创建HttpPost对象。

3. 如果需要发送请求参数,

    可调用HttpGetHttpPost共同的setParams(HetpParams params)方法来添加请求参数;

    对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。

4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse

5. 调用HttpResponsegetAllHeaders()getHeaders(String name)等方法可获取服务器的响应头;

调用HttpResponsegetEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。

程序可通过该对象获取服务器的响应内容。

6. 释放连接。无论执行方法是否成功,都必须释放连接

 

1.不带参数发送请求

//1创建一个客户端

      CloseableHttpClient httpClient = HttpClients.createDefault();

      //2创建一个get方法请求实例

      HttpPut put = new HttpPut("http://www.baidu.com");

      //3发送请求

      CloseableHttpResponse response = httpClient.execute(put);

      //4获取响应的头信息

      System.out.println(response.getStatusLine());

      //5获取响应的主题内容

      HttpEntity entity = response.getEntity();

      //转换响应体

      String content = EntityUtils.toString(entity);

      System.out.println(content);

      //6关闭响应对象

      response.close();

--------------------------------------------------------------------

2.带参数发送请求

//1 创建浏览器对象

      CloseableHttpClient httpClient = HttpClients.createDefault();

      //传递参数

//    List list = new ArrayList();

//    list.add(new BasicNameValuePair("username", "cgx"));

//    list.add(new BasicNameValuePair("password", "123456"));

//    String params = EntityUtils.toString(new UrlEncodedFormEntity(list), Consts.UTF_8);

      //2 创建get方法实例

      HttpGet get = new HttpGet("http://localhost:8088/jx_web/login?username=cgx&password=123456");

      //3 发送请求

      CloseableHttpResponse response = httpClient.execute(get);

      //4 获取响应状态

      StatusLine line = response.getStatusLine();

      System.out.println(line);

      //5 获取响应体

      HttpEntity entityresponse.getEntity();

      String content = EntityUtils.toString(entity);

      System.out.println(content);

      //6 关闭连接

      response.close();

 

-----------------------------------------------------------------------

3.使用-发送post请求

//创建httpClient对象

      CloseableHttpClient httpClient = HttpClients.createDefault();

     

      //创建post方法实例

      HttpPost httpPost = new HttpPost("http://localhost:8088/web/login");

     

      //设置参数

      List list = new ArrayList();

      list.add(new BasicNameValuePair("username", "aa"));

      list.add(new BasicNameValuePair("password", "123456"));

      UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list);

      httpPost.setEntity(entity);

      //发送请求

      CloseableHttpResponse response = httpClient.execute(httpPost);

      //接收状态信息

      System.out.println(response.getStatusLine());

      //接收信息体

      HttpEntity httpEntity = response.getEntity();

      String content = EntityUtils.toString(httpEntity);

      System.out.println(content);

      //关闭

      response.close();

你可能感兴趣的:(java)