HttpClient4.3.1 HttpPost .

字体大小: 中小 标准 ->行高大小: 标准

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.             closeableHttpClient.close();  
  2.         } catch (IOException e) {  
  3.             e.printStackTrace();  
  4.         }  
  5.     }  
  6. }  

 


以下内容来自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响应信息

 

 public static void main(String args[]) {
        //创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        //HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

        HttpPost httpPost = new HttpPost("http://tutor.sturgeon.mopaas.com/tutor/search");
        httpPost.setConfig(DEFAULT);
        // 创建参数队列
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("searchText", "英语"));

        UrlEncodedFormEntity entity;
        try {
            entity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httpPost.setEntity(entity);

            HttpResponse httpResponse;
            //post请求
            httpResponse = closeableHttpClient.execute(httpPost);

            //getEntity()
            HttpEntity httpEntity = httpResponse.getEntity();
            if (httpEntity != null) {
                //打印响应内容
                System.out.println("response:" + EntityUtils.toString(httpEntity, "UTF-8"));
            }
            //释放资源
            closeableHttpClient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

 

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/70079.html