HttpClient use process

 manual

  • Creating  HttpClient Objects
  • Request method creates an instance, and specify the requested URL. If you need to send a GET request to create  HttpGet an object; if you need to send a POST request to create  HttpPost an object.
  • To send the request parameters, it can be called  HttpGet, HttpPost a common  setParams(HttpParams params) way to add a request parameter; for  HttpPost purposes of object can call  setEntity(HttpEntity entity) the method set request parameters.
  • Calling  HttpClient object  execute(HttpUriRequest request) sends a request, the method returns a  HttpResponse.
  • Calling  HttpResponse of  getAllHeaders(), getHeaders(String name) or the like can be obtained in response to the first server; call  HttpResponse the  getEntity() method obtains  HttpEntity object content server in response to the package. Program available through the content server in response to the object.
  • Release the connection. Whether the implementation of the method is successful, the connection must be released

 

Case

  • pom.xml Configuration is as follows:
    <!-- Apache Http Begin -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>fluent-hc</artifactId>
        <version>4.5.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.5.5</version>
    </dependency>
    <!-- Apache Http End -->

     

  • Creating HttpGet request

    package com.funtl.hello.httpclient;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    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;
    
    import java.io.IOException;
    
    public class MyTest {
        public static void main(String[] args) {
            get();
        }
    
        private static void get() {
            // 创建 HttpClient 客户端
            CloseableHttpClient httpClient = HttpClients.createDefault();
    
            // 创建 HttpGet 请求
            HttpGet httpGet = new HttpGet("http://localhost:8080/content/page?draw=1&start=0&length=10");
            // 设置长连接
            httpGet.setHeader("Connection", "keep-alive");
            // 设置代理(模拟浏览器版本)
            httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
            // 设置 Cookie
            httpGet.setHeader("Cookie", "UM_distinctid=16442706a09352-0376059833914f-3c604504-1fa400-16442706a0b345; CNZZDATA1262458286=1603637673-1530123020-%7C1530123020; JSESSIONID=805587506F1594AE02DC45845A7216A4");
    
            CloseableHttpResponse httpResponse = null;
            try {
                // 请求并获得响应结果
                httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                // 输出请求结果
                System.out.println(EntityUtils.toString(httpEntity));
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            // 无论如何必须关闭连接
            finally {
                if (httpResponse != null) {
                    try {
                        httpResponse.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
                if (httpClient != null) {
                    try {
                        httpClient.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

     

  • Creating HttpPost request

    package com.funtl.hello.httpclient;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class MyTest {
        public static void main(String[] args) {
            post();
        }
    
        private static void post() {
            // 创建 HttpClient 客户端
            CloseableHttpClient httpClient = HttpClients.createDefault();
    
            // 创建 HttpPost 请求
            HttpPost httpPost = new HttpPost("http://localhost:8080/content/page");
            // 设置长连接
            httpPost.setHeader("Connection", "keep-alive");
            // 设置代理(模拟浏览器版本)
            httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
            // 设置 Cookie
            httpPost.setHeader("Cookie", "UM_distinctid=16442706a09352-0376059833914f-3c604504-1fa400-16442706a0b345; CNZZDATA1262458286=1603637673-1530123020-%7C1530123020; JSESSIONID=805587506F1594AE02DC45845A7216A4");
    
            // 创建 HttpPost 参数
            List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
            params.add(new BasicNameValuePair("draw", "1"));
            params.add(new BasicNameValuePair("start", "0"));
            params.add(new BasicNameValuePair("length", "10"));
    
            CloseableHttpResponse httpResponse = null;
            try {
                // 设置 HttpPost 参数
                httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
                httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                // 输出请求结果
                System.out.println(EntityUtils.toString(httpEntity));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            // 无论如何必须关闭连接
            finally {
                try {
                    if (httpResponse != null) {
                        httpResponse.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                try {
                    if (httpClient != null) {
                        httpClient.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

     

Published 158 original articles · won praise 26 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_41650354/article/details/104004287