HttpClient详细使用

一、简介

       HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议

二、特性

  1、基于标准的java语言

  2、以可扩展的面向对象的结构实现了GET、POST、PUT等Http的全部方法

  3、支持https协议

  4、 Request的输出流可以避免流中内容直接缓冲到socket服务器。

  5、Response的输入流可以有效的从socket服务器直接读取相应内容。

三、使用方法

  

  1、创建HttpClient对象。(推荐用CloseableHttpClient,HttpClient是历史遗留版本,官方推荐使用CloseableHttpClient)

**注:两者的依赖不同

  2、创建请求方法的实例,并指定请求uri。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。

  3、如果需要发送请求参数,直接在请求地址后拼接参数(√);对于HttpPost对象而言,也可先设置参数队列,然后调用setEntity(HttpEntity entity)方法来设置请求参数。

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

  5、调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,如果出现乱码,可以将HttpEntity对象改成StringEntity,可通过该对象获取服务器的响应内容。

  6、释放连接,无论方法是否执行成功,都应当必须释放

所需依赖maven

<!--HttpClient-->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>

<!--CloseableHttpClient-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>

四、案例

     以get、post、文件上传举例

Post:(有参数)

```java
/** 
* 表单提交(未封装方法) 
*/   

public class Second {
    // *创建默认的httpClient实例(CloseableHttpClient).
    static CloseableHttpClient httpclient = HttpClients.createDefault();
    public static void main(String[] args){
    // *创建HttpPost
    HttpPost httppost = new HttpPost("http://localhost:8080/warnWeathers/save");
    // *创建参数队列
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("winfo", "红色降雨预警,请勿随意外出游玩"));
        nvps.add(new BasicNameValuePair("wtype", "红色降雨预警"));
        nvps.add(new BasicNameValuePair("warea", "香洲,斗门"));
        nvps.add(new BasicNameValuePair("wdays", "2"));
     UrlEncodedFormEntity uefEntity;
        try {
//          设置实体类参数编码格式
        uefEntity = new UrlEncodedFormEntity(nvps, "UTF-8");
//        *发送请求参数
        httppost.setEntity(uefEntity);
//        测试:输出请求地址
//        System.out.println("executing request " + httppost.getURI());
//       *调用HttpClient对象的execute发送请求方法并返回一个httpClient/CloseableHttpResponse对象
        CloseableHttpResponse response = httpclient.execute(httppost);
        try {
//       *调用HttpResponse的getEntity()方法可获取HttpEntity对象
//       HttpEntity-->StringEntity  解决乱码问题
            HttpEntity entity = response.getEntity();
//       获取响应内容以及响应头
            String message = EntityUtils.toString(entity, "UTF-8");
            Header[] allHeaders = response.getAllHeaders();
            if (message != null) {
                System.out.println("响应头:"+allHeaders);
                System.out.println("Response content: " + message);
            }
//       *关闭连接,释放资源            
        } finally {
            response.close();
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {

        try {
            httpclient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Post请求示例代码

文件上传

/** 
* 上传文件 
*/   
public void upload() {   
   CloseableHttpClient httpclient = HttpClients.createDefault();   
     try {   
   HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/uploads.action");   
   FileBody filebody = new FileBody(new File("D:\image\1.jpg"));   
   StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);   
   HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("filebody", filebody).addPart("comment", comment).build();   
            httppost.setEntity(reqEntity);   
            System.out.println("executing request " + httppost.getRequestLine()); 
            CloseableHttpResponse response = httpclient.execute(httppost);   
            try {     
                System.out.println(response.getStatusLine());   
                HttpEntity resEntity = response.getEntity();   
                if (resEntity != null) {   
                    System.out.println("Response content length: " + resEntity.getContentLength());   
                }   
                EntityUtils.consume(resEntity);   
            } finally {   
                response.close();   
            }   
        } catch (ClientProtocolException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        } finally {   
            try {   
                httpclient.close();   
            } catch (IOException e) {   
                e.printStackTrace();   
            }   
        }   
    }
文件上传示例代码

Get

/**
*无参Get请求方法
*带参的可直接在请求地址后拼接
*/

public class First {
// *创建默认的httpClient实例(CloseableHttpClient).
    static CloseableHttpClient httpclient = HttpClients.createDefault();
    public static void main(String[] args) throws Exception{     
//  *创建HttpGet请求        
        HttpGet httpGet = new HttpGet("http://localhost:8080/warnWeathers/findAll");
//  *执行get请求
        HttpResponse response = httpclient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() == 200) {
//  *调用HttpResponse的getEntity()方法可获取HttpEntity对象            
            HttpEntity resEntity = response.getEntity();
            String message = EntityUtils.toString(resEntity, "utf-8");
            //根据所需自行
            System.out.println("状态码:"+response.getStatusLine().getStatusCode());
            System.out.println("响应状态:"+response.getStatusLine());
            System.out.println("地区:"+response.getLocale());
            System.out.println("返回数据类型:"+resEntity.getContentType());
            System.out.println("响应内容:"+message);
        } else {
            System.out.println("出错啦,快去找一下原因!状态码是:"+response.getStatusLine().getStatusCode());
        }
    }
}
Get请求示例代码

附:

     关于RequestConfig配置

//创建RequestConfig
RequestConfig defaultRequestConfig = RequestConfig.custom()
    //一、连接超时,指的是连接一个url的连接等待时间
        .setConnectTimeout(1000)
    // 二、读取数据超时,指的是连接上一个url,获取response的返回等待时间
        .setSocketTimeout(1000)
    //三、从连接池获取连接的超时时间
        .setConnectionRequestTimeout(5000)
        .build();

// 这个超时可以设置为客户端级别,作为所有请求的默认值: CloseableHttpClient httpclient = HttpClients.custom() .setDefaultRequestConfig(defaultRequestConfig) .build(); // httpclient.execute(httppost)可以让httppost直接享受到httpclient中的默认配置. // Request不会继承客户端级别的请求配置,所以在自定义Request的时候,需要将客户端的默认配置拷贝过去: HttpGet httpget = new HttpGet("http://www.baidu.com/"); RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig) .setProxy(new HttpHost("myotherproxy", 8080)) .build(); httpget.setConfig(requestConfig); // httpget可以单独地使用新copy的requestConfig请求配置,不会对别的request请求产生影响

 

 

原文地址:https://www.cnblogs.com/winton-nfs/p/14071961.html