Java request Http interface (super detailed - with tool class)


I don’t need to say more about the importance of the Http protocol. Compared with the URLConnection that comes with the traditional JDK, HttpClient has increased ease of use and flexibility (the specific differences will be discussed in the future). It is easy, and it is also convenient for developers to test the interface (based on the Http protocol), which not only improves the efficiency of development, but also facilitates the robustness of the code. Therefore, mastering HttpClient proficiently is a very important compulsory content. After mastering HttpClient, I believe that the understanding of the Http protocol will be more in-depth.

1. Introduction

HttpClient is a sub-project under Apache Jakarta Common, which is used to provide an efficient, up-to-date, feature-rich client programming toolkit supporting the HTTP protocol, and it supports the latest version and recommendations of the HTTP protocol. HttpClient has been used in many projects, such as Cactus and HTMLUnit, two other well-known open source projects on Apache Jakarta, both use HttpClient.

Download link: http://hc.apache.org/downloads.cgi

Two, characteristics

  1. Based on standard, pure java language. Implemented Http1.0 and Http1.1

  2. All Http methods (GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE) are implemented in an extensible object-oriented structure.

  3. Support HTTPS protocol.

  4. A transparent connection is established through an Http proxy.

  5. Use the CONNECT method to establish a tunneled https connection through the Http proxy.

  6. Basic, Digest, NTLMv1, NTLMv2, NTLM2 Session, SNPNEGO/Kerberos authentication schemes.

  7. Plug-in custom authentication scheme.

  8. Portable and reliable socket factories make it easier to use third-party solutions.

  9. The connection manager supports multi-threaded applications. Supports setting the maximum number of connections, and supports setting the maximum number of connections for each host, and discovers and closes expired connections.

  10. Automatically handle cookies in Set-Cookie.

  11. Plug-in custom cookie policy.

  12. The output stream of Request can avoid the content in the stream from being directly buffered to the socket server.

  13. The input stream of Response can effectively read the corresponding content directly from the socket server.

  14. Use KeepAlive to maintain persistent connections in http1.0 and http1.1.

  15. Directly get the response code and headers sent by the server.

  16. Ability to set connection timeout.

  17. Experimental support for http1.1 response caching.

  18. The source code is freely available under the Apache License.

3. How to use

It is very simple to use HttpClient to send requests and receive responses. Generally, the following steps are required.

  1. Create an HttpClient object. The latest version of httpClient uses the implementation class closeableHTTPClient, and the previous default is invalid.

  2. Create an instance of the request method and specify the request URL. If you need to send a GET request, create an HttpGet object; if you need to send a POST request, create an HttpPost object.

  3. If you need to send request parameters, you can call the common setParams(HetpParams params) method of HttpGet and HttpPost to add request parameters; for HttpPost objects, you can also call the setEntity(HttpEntity entity) method to set request parameters.

  4. Call execute(HttpUriRequest request) of the HttpClient object to send the request, and this method returns an HttpResponse.

  5. Call the getAllHeaders() and getHeaders(String name) methods of HttpResponse to get the response header of the server; call the getEntity() method of HttpResponse to get the HttpEntity object, which wraps the response content of the server. The program can obtain the response content of the server through this object.

  6. Release the connection. The connection must be released regardless of the success of the execution method

Environment Description: Eclipse, JDK1.8, SpringBoot

Four: Sample code:

1: Guide package:

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

    <!--fafastjson-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.71</version>
    </dependency>

Note: The purpose of introducing this dependency is that in subsequent examples, the "function of converting objects into json strings" will be used, and other dependencies with this function can also be introduced.

Detailed usage example
Declaration: In this example, HttpClient is sent in JAVA (sent by the unit test in test); it is also received in JAVA (received in controller).

Disclaimer: The code below is valid in my personal test.

2: get request -params pass parameter

 public static String doHttpGet(String url, Map<String,String> params,Map<String,String> headParams) {
    
    
        String result = null;
        //1.获取httpclient
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //接口返回结果
        CloseableHttpResponse response = null;
        String paramStr = null;
        try {
    
    
            List<BasicNameValuePair> paramsList = new ArrayList<BasicNameValuePair>();

            for (String key : params.keySet()) {
    
    
                paramsList.add(new BasicNameValuePair(key,params.get(key)));
            }
            paramStr = EntityUtils.toString(new UrlEncodedFormEntity(paramsList));
            //拼接参数
            StringBuffer sb = new StringBuffer();
            sb.append(url);
            sb.append("?");
            sb.append(paramStr);

            //2.创建get请求
            HttpGet httpGet = new HttpGet(sb.toString());
            //3.设置请求和传输超时时间
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
            httpGet.setConfig(requestConfig);
            /*此处可以添加一些请求头信息,例如:
            httpGet.addHeader("content-type","text/xml");*/
            for (String head : headParams.keySet()) {
    
    
                httpGet.addHeader(head,headParams.get(head));
            }
            //4.提交参数
            response = httpClient.execute(httpGet);
            //5.得到响应信息
            int statusCode = response.getStatusLine().getStatusCode();
            //6.判断响应信息是否正确
            if (HttpStatus.SC_OK != statusCode) {
    
    
                //终止并抛出异常
                httpGet.abort();
                throw new RuntimeException("HttpClient,error status code :" + statusCode);
            }
            //7.转换成实体类
            HttpEntity entity = response.getEntity();
            if (null != entity) {
    
    
                result = EntityUtils.toString(entity);
            }
            EntityUtils.consume(entity);
        } catch (UnsupportedEncodingException e) {
    
    
            e.printStackTrace();
        } catch (ClientProtocolException e) {
    
    
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            //8.关闭所有资源连接
            if (null != response) {
    
    
                try {
    
    
                    response.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
    
    
                try {
    
    
                    httpClient.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

3: post request -params pass parameter

 /**
     * http post 请求
     */
    public static String doPost(String url, Map<String,String> params,Map<String,String> headParams) {
    
    
        String result = null;
        //1. 获取httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
    
    
            //2. 创建post请求
            HttpPost httpPost = new HttpPost(url);

            //3.设置请求和传输超时时间
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(100000).setConnectTimeout(100000).build();
            httpPost.setConfig(requestConfig);

            //4.提交参数发送请求
            List<BasicNameValuePair> paramsList = new ArrayList<>();
            for (String key : params.keySet()) {
    
    
                paramsList.add(new BasicNameValuePair(key,params.get(key)));
            }
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(paramsList, HTTP.UTF_8);
            httpPost.setEntity(urlEncodedFormEntity);
            //设置请求头
            for (String head : headParams.keySet()) {
    
    
                httpPost.addHeader(head,headParams.get(head));
            }

            response = httpClient.execute(httpPost);

            //5.得到响应信息
            int statusCode = response.getStatusLine().getStatusCode();
            //6. 判断响应信息是否正确
            if (HttpStatus.SC_OK != statusCode) {
    
    
                //结束请求并抛出异常
                httpPost.abort();
                throw new RuntimeException("HttpClient,error status code :" + statusCode);
            }
            //7. 转换成实体类
            HttpEntity entity = response.getEntity();
            if (null != entity) {
    
    
                result = EntityUtils.toString(entity, "UTF-8");
            }
            EntityUtils.consume(entity);
        } catch (UnsupportedEncodingException e) {
    
    
            e.printStackTrace();
        } catch (ClientProtocolException e) {
    
    
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            //8. 关闭所有资源连接
            if (null != response) {
    
    
                try {
    
    
                    response.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
    
    
                try {
    
    
                    httpClient.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

4: post request - body parameter transfer

 public static String doPostJson(String url, String params,Map<String,String> headParams) {
    
    
        String result = null;
        //1. 获取httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
    
    
            //2. 创建post请求
            HttpPost httpPost = new HttpPost(url);

            //3.设置请求和传输超时时间
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(100000).setConnectTimeout(100000).build();
            httpPost.setConfig(requestConfig);

            //4.提交参数发送请求
            httpPost.setEntity(new StringEntity(params, ContentType.create("application/json", "utf-8")));

            //设置请求头
            for (String head : headParams.keySet()) {
    
    
                httpPost.addHeader(head,headParams.get(head));
            }

            response = httpClient.execute(httpPost);

            //5.得到响应信息
            int statusCode = response.getStatusLine().getStatusCode();
            //6. 判断响应信息是否正确
            if (HttpStatus.SC_OK != statusCode) {
    
    
                //结束请求并抛出异常
                httpPost.abort();
                throw new RuntimeException("HttpClient,error status code :" + statusCode);
            }
            //7. 转换成实体类
            HttpEntity entity = response.getEntity();
            if (null != entity) {
    
    
                result = EntityUtils.toString(entity, "UTF-8");
            }
            EntityUtils.consume(entity);
        } catch (UnsupportedEncodingException e) {
    
    
            e.printStackTrace();
        } catch (ClientProtocolException e) {
    
    
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            //8. 关闭所有资源连接
            if (null != response) {
    
    
                try {
    
    
                    response.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
    
    
                try {
    
    
                    httpClient.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

5: Note: If you want to transfer files:

 //文件URL,此处取豆瓣上的一个图片
        String fileUrl ="https://img1.doubanio.com/view/photo/l/public/p2537149328.webp";
        
            //提取到文件名
            String fileName = fileUrl.substring(fileUrl.lastIndexOf("/")+1);
            //转换成文件流
            InputStream is = new URL(fileUrl).openStream();

            //接收文件的服务器地址
            String uploadURL = "http://localhost:8003/fileupload";

            //创建HttpClient
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(uploadURL);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            /*绑定文件参数,传入文件流和contenttype,此处也可以继续添加其他formdata参数*/
            builder.addBinaryBody("file",is, ContentType.MULTIPART_FORM_DATA,fileName);
            HttpEntity entity = builder.build();
            httpPost.setEntity(entity);

Five: The difference between HttpClient and CloseableHttpClient - now basically 4.3 or above

httpclient3.x


		HttpClient client = new HttpClient();
		// 设置代理服务器地址和端口
		// client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port);
		// 使用 GET 方法 ,如果服务器需要通过 HTTPS 连接,那只需要将下面 URL 中的 http 换成 https
		HttpMethodmethod = new GetMethod("http://java.sun.com");
		// 使用POST方法
		// HttpMethod method = new PostMethod("http://java.sun.com");
		client.executeMethod(method);
		// 打印服务器返回的状态
		System.out.println(method.getStatusLine());
		// 打印返回的信息
		System.out.println(method.getResponseBodyAsString());
		// 释放连接
		method.releaseConnection();

httpclient4.x to below httpclient4.3

public void getUrl(String url, String encoding) throws ClientProtocolException, IOException {
    
    
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(url);
		HttpResponse response = client.execute(get);
		HttpEntity entity = response.getEntity();
		if (entity != null) {
    
    
			InputStream instream = entity.getContent();
			try {
    
    
				BufferedReader reader = new BufferedReader(new InputStreamReader(instream, encoding));
				System.out.println(reader.readLine());
			} catch (Exception e) {
    
    
				e.printStackTrace();
			} finally {
    
    
				instream.close();
			}
		}
		// 关闭连接.
		client.getConnectionManager().shutdown();
	}
 

httpclient4.3 or higher

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;


public static String getResult(String urlStr) {
    
    
		CloseableHttpClient httpClient = HttpClients.createDefault();
		// HTTP Get请求
		HttpGet httpGet = new HttpGet(urlStr);
		// 设置请求和传输超时时间
		// RequestConfig requestConfig =
		// RequestConfig.custom().setSocketTimeout(TIME_OUT).setConnectTimeout(TIME_OUT).build();
		// httpGet.setConfig(requestConfig);
		String res = "";
		try {
    
    
			// 执行请求
			HttpResponse getAddrResp = httpClient.execute(httpGet);
			HttpEntity entity = getAddrResp.getEntity();
			if (entity != null) {
    
    
				res = EntityUtils.toString(entity);
			}
			log.info("响应" + getAddrResp.getStatusLine());
		} catch (Exception e) {
    
    
			log.error(e.getMessage(), e);
			return res;
		} finally {
    
    
			try {
    
    
				httpClient.close();
			} catch (IOException e) {
    
    
				log.error(e.getMessage(), e);
				return res;
			}
		}
		return res;
	}

Guess you like

Origin blog.csdn.net/qq_41694906/article/details/126748497