zoukankan      html  css  js  c++  java
  • HttpClient基本用法

    《Apache HttpClient 4.3开发指南》

    Apache HttpClient 4系列已经发布很久了,但由于它与HttpClient 3.x版本完全不兼容,以至于业内采用此库的公司较少,在互联网上也少有相关的文档资料分享。

    本文旨在写一个简要的Apache HttpClient 4.3开发指南,帮助开发者快速上手Apache HttpClient 4.3.x库。

    要注意的是,本文档中的代码在低于HttpClient 4.3版本的地方可能不能运行。

    二、开发手册

    1、创建HTTP客户端

    1. CloseableHttpClient client = HttpClientBuilder.create().build();  


    2、发送基本的GET请求

    1. instance.execute(new HttpGet(“http://www.baidu.com”));  


    3、获取HTTP响应的状态码

    1. String url = “http://www.baidu.com”;  
    2. CloseableHttpResponse response = instance.execute(new HttpGet(url));  
    3. assertThat(response.getStatusLine().getStatusCode(), equalTo(200));  


    4、获取响应的媒体类型

    1. String url = “http://www.baidu.com”;  
    2. CloseableHttpResponse response = instance.execute(new HttpGet(url));  
    3. String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType();  
    4. assertThat(contentMimeType, equalTo(ContentType.TEXT_HTML.getMimeType()));  


    5、获取响应的BODY部分

    1. String url = “http://www.baidu.com”;  
    2. CloseableHttpResponse response = instance.execute(new HttpGet(url));  
    3. String bodyAsString = EntityUtils.toString(response.getEntity());  
    4. assertThat(bodyAsString, notNullValue());  


    6、配置请求的超时设置

    1. @Test(expected=SocketTimeoutException.class)  
    2. public void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws ClientProtocolException, IOException{  
    3.     RequestConfig requestConfig = RequestConfig.custom()  
    4.     .setConnectionRequestTimeout(50).setConnectTimeout(50)  
    5.     .setSocketTimeout(50).build();  
    6.     HttpGet request = new HttpGet(SAMPLE_URL);  
    7.     request.setConfig(requestConfig);  
    8.     instance.execute(request);  
    9. }  


    7、发送POST请求

    1. instance.execute(new HttpPost(SAMPLE_URL));  


    8、为HTTP请求配置重定向

    1. CloseableHttpClient instance = HttpClientBuilder.create().disableRedirectHandling().build();  
    2. CloseableHttpResponse response = instance.execute(new HttpGet(SAMPLE_URL));  
    3. assertThat(reponse.getStatusLine().getStatusCode(), equalTo(301));  


    9、配置请求的HEADER部分

    1. HttpGet request = new HttpGet(SAMPLE_URL);  
    2. request.addHeader(HttpHeaders.ACCEPT, “application/xml”);  
    3. response = instance.execute(request);  


    10、获取响应的HEADER部分

    1. CloseableHttpResponse response = instance.execute(new HttpGet(SAMPLE_URL));  
    2. Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE);  
    3. assertThat(headers, not(emptyArray()));  


    11、关闭或释放资源

    1. response = instance.execute(new HttpGet(SAMPLE_URL));  
    2. try{  
    3.   HttpEntity entity = response.getEntity();  
    4.   if(entity!=null){  
    5. InputStream instream = entity.getContent();  
    6. instream.close();  
    7.   }  
    8. finally{  
    9.   response.close();  
    10. }  


    以上内容涵盖了HttpClient 4.3所有常见的需求,供开发者参考。

  • 相关阅读:
    回调函数中调用类中的非静态成员变量或非静态成员函数
    [NewCoder]复杂链表的复制
    C++对象模型--总结
    chunk writer 中需要对抛错的交易进行回滚,同时又要在其他表中记录是哪一笔交易记录失败
    为什么因式分解n=pq分别得到pq是求解密钥中d的关键
    DB2 创建数据库
    socket 收发报文小程序
    Zbrush Topogun 备忘
    过度科目理解
    借贷记账思考2015.12.28
  • 原文地址:https://www.cnblogs.com/Rozdy/p/4895080.html
Copyright © 2011-2022 走看看