HttpClient4.3.1简单入门实例

79 篇文章 0 订阅

一、

1、先看一下示例代码

  1. public class HttpClientTest {  
  2.     public static void main(String args[]) {  
  3.         //创建HttpClientBuilder  
  4.         HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();  
  5.         //HttpClient  
  6.         CloseableHttpClient closeableHttpClient = httpClientBuilder.build();  
  7.   
  8.         HttpGet httpGet = new HttpGet("http://www.gxnu.edu.cn/default.html");  
  9.         System.out.println(httpGet.getRequestLine());  
  10.         try {  
  11.             //执行get请求  
  12.             HttpResponse httpResponse = closeableHttpClient.execute(httpGet);  
  13.             //获取响应消息实体  
  14.             HttpEntity entity = httpResponse.getEntity();  
  15.             //响应状态  
  16.             System.out.println("status:" + httpResponse.getStatusLine());  
  17.             //判断响应实体是否为空  
  18.             if (entity != null) {  
  19.                 System.out.println("contentEncoding:" + entity.getContentEncoding());  
  20.                 System.out.println("response content:" + EntityUtils.toString(entity));  
  21.             }  
  22.         } catch (IOException e) {  
  23.             e.printStackTrace();  
  24.         } finally {  
  25.             try {  
  1.             //关闭流并释放资源  
  2.             closeableHttpClient.close();  
  3.         } catch (IOException e) {  
  4.             e.printStackTrace();  
  5.         }  
  6.     }  
  7. }  


以下内容来自 HttpClient4.3.1 API文档: http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/overview-summary.html


2、HttpClientBuilder

HttpClientBuilder用于创建CloseableHttpClient实例。看了一下API文档,AbstractHttpClient AutoRetryHttpClient DefaultHttpClient等都被弃用了,使用HttpClientBuilder代替。

3、CloseableHttpClient

实现接口:CloseableAutoCloseableHttpClient;子类:AbstractHttpClient

4、HttpGet

非线程安全;HttpGet有三个构造方法:HttpGet()、HttpGet(String uri)、HttpGet(URI uri)

5、HttpResponse

服务器在接收和解释请求之后返回一个HTTP响应信息

     Response      = Status-Line
                     *(( general-header
                      | response-header
                      | entity-header ) CRLF)
                     CRLF
                     [ message-body ]
 

二、使用HttpClient Post提交数据,详细代码注释

  1. public static void main(String args[]) {  
  2.        //创建HttpClientBuilder  
  3.        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();  
  4.        //HttpClient  
  5.        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();  
  6.   
  7.        HttpPost httpPost = new HttpPost("http://tutor.sturgeon.mopaas.com/tutor/search");  
  8.        httpPost.setConfig(DEFAULT);  
  9.        // 创建参数队列  
  10.        List<NameValuePair> formparams = new ArrayList<NameValuePair>();  
  11.        formparams.add(new BasicNameValuePair("searchText""英语"));  
  12.   
  13.        UrlEncodedFormEntity entity;  
  14.        try {  
  15.            entity = new UrlEncodedFormEntity(formparams, "UTF-8");  
  16.            httpPost.setEntity(entity);  
  17.   
  18.            HttpResponse httpResponse;  
  19.            //post请求  
  20.            httpResponse = closeableHttpClient.execute(httpPost);  
  21.   
  22.            //getEntity()  
  23.            HttpEntity httpEntity = httpResponse.getEntity();  
  24.            if (httpEntity != null) {  
  25.                //打印响应内容  
  26.                System.out.println("response:" + EntityUtils.toString(httpEntity, "UTF-8"));  
  27.            }  
  28.            //释放资源  
  29.            closeableHttpClient.close();  
  30.        } catch (Exception e) {  
  31.            e.printStackTrace();  
  32.        }  
  33.   
  34.    } 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值