Use of HttpClient and Notices

First, introduce some good websites:

Introduction to httpclient: http://www.ibm.com/developerworks/cn/opensource/os-httpclient/ 
HTTP client certificate import: http://www.blogjava.net/happytian/archive/2006/12/22/89447.html
Advanced knowledge of httpclient: http://laohuang.iteye.com/blog/55613
HTTP client official document: http://hc.apache.org/http components-client/index.html
HTTP client resource closure: http://www.iteye.com/topic/234759
Examples: http://blog.csdn.net/z69183787/article/details/19153369

And then, that's the sentence... Good things to reproduce, learn one can not be less, below Turn from here

I believe that the importance of the Http protocol does not need to say much. Compared with the URLConnection provided by traditional JDK, HttpClient adds ease of use and flexibility (specific differences, we will discuss later). It is not only easier for clients to send Http requests, but also for developers. test Interface (based on Http protocol) improves the efficiency of development and the robustness of code. Therefore, mastering HttpClient is a very important required content. After mastering HttpClient, I believe that the understanding of Http protocol will be more in-depth.

I. Introduction

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

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

Two, characteristics

1. Standard-based, pure Java Language. Implementation of Http1.0 and Http1.1

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

3. Support HTTPS protocol.

4. Establish transparent connections through Http proxy.

5. Use CONNECT method to establish tunnel https connection through Http proxy.

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

7. Plug-in custom authentication scheme.

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

9. Connection Manager supports multi-threaded applications. Supports setting the maximum number of connections, and also supports setting the maximum number of connections per host, discovering and closing outdated connections.

10. Automatically process Cookies in Set-Cookie.

11. Plug-in custom Cookie policy.

12. Request's output stream prevents content from being buffered directly to the socket server.

13. Response's input stream can effectively read the corresponding content directly from the socket server.

14. KeepAlive is used to maintain persistent connections in HTTP 1.0 and HTTP 1.1.

15. Get the response code and headers sent by the server directly.

16. Set the connection timeout capability.

17. Experimental support http1.1 response caching.

18. The source code is freely available based on Apache License.

III. Usage

Using HttpClient to send requests and receive responses is very simple. Generally, the following steps are needed.

1. Create the HttpClient object.

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

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

4. Call the execute(HttpUriRequest request) of the HttpClient object to send the request, which returns a HttpResponse.

5. The server's response header can be obtained by calling the getAllHeaders(), getHeaders(String name) of HttpResponse, and the HttpResponse's getEntity() method can be used to obtain the HttpEntity object, which wraps the server's response content. The program can get the response content of the server through this object.

6. Release the connection. Whether or not the execution method succeeds, the connection must be released

Four. Example

  1.   
  2. public class HttpClientTest {  
  3.   
  4.     @Test  
  5.     public void jUnitTest() {  
  6.         get();  
  7.     }  
  8.   
  9.     /** 
  10.      * HttpClient Connect SSL 
  11.      */  
  12.     public void ssl() {  
  13.         CloseableHttpClient httpclient = null;  
  14.         try {  
  15.             KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());  
  16.             FileInputStream instream = new FileInputStream(new File("d:\\tomcat.keystore"));  
  17.             try {  
  18.                 //Load keyStore d:\tomcat.keystore  
  19.                 trustStore.load(instream, "123456".toCharArray());  
  20.             } catch (CertificateException e) {  
  21.                 e.printStackTrace();  
  22.             } finally {  
  23.                 try {  
  24.                     instream.close();  
  25.                 } catch (Exception ignore) {  
  26.                 }  
  27.             }  
  28.             //Believe in your CA and all self-signed certificates  
  29.             SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();  
  30.             //Only TLSv1 protocol is allowed  
  31.             SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,  
  32.                     SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);  
  33.             httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();  
  34.             //Create http requests (get mode)  
  35.             HttpGet httpget = new HttpGet("https://localhost:8443/myDemo/Ajax/serivceJ.action");  
  36.             System.out.println("executing request" + httpget.getRequestLine());  
  37.             CloseableHttpResponse response = httpclient.execute(httpget);  
  38.             try {  
  39.                 HttpEntity entity = response.getEntity();  
  40.                 System.out.println("----------------------------------------");  
  41.                 System.out.println(response.getStatusLine());  
  42.                 if (entity != null) {  
  43.                     System.out.println("Response content length: " + entity.getContentLength());  
  44.                     System.out.println(EntityUtils.toString(entity));  
  45.                     EntityUtils.consume(entity);  
  46.                 }  
  47.             } finally {  
  48.                 response.close();  
  49.             }  
  50.         } catch (ParseException e) {  
  51.             e.printStackTrace();  
  52.         } catch (IOException e) {  
  53.             e.printStackTrace();  
  54.         } catch (KeyManagementException e) {  
  55.             e.printStackTrace();  
  56.         } catch (NoSuchAlgorithmException e) {  
  57.             e.printStackTrace();  
  58.         } catch (KeyStoreException e) {  
  59.             e.printStackTrace();  
  60.         } finally {  
  61.             if (httpclient != null) {  
  62.                 try {  
  63.                     httpclient.close();  
  64.                 } catch (IOException e) {  
  65.                     e.printStackTrace();  
  66.                 }  
  67.             }  
  68.         }  
  69.     }  
  70.   
  71.     /** 
  72.      * post Form submission (simulated user login request) 
  73.      */  
  74.     public void postForm() {  
  75.         //Create a default httpClient instance.  
  76.         CloseableHttpClient httpclient = HttpClients.createDefault();  
  77.         //Create httppost  
  78.         HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceJ.action");  
  79.         //Create parameter queues  
  80.         List<namevaluepair> formparams = new ArrayList<namevaluepair>();  
  81.         formparams.add(new BasicNameValuePair("username""admin"));  
  82.         formparams.add(new BasicNameValuePair("password""123456"));  
  83.         UrlEncodedFormEntity uefEntity;  
  84.         try {  
  85.             uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");  
  86.             httppost.setEntity(uefEntity);  
  87.             System.out.println("executing request " + httppost.getURI());  
  88.             CloseableHttpResponse response = httpclient.execute(httppost);  
  89.             try {  
  90.                 HttpEntity entity = response.getEntity();  
  91.                 if (entity != null) {  
  92.                     System.out.println("--------------------------------------");  
  93.                     System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));  
  94.                     System.out.println("--------------------------------------");  
  95.                 }  
  96.             } finally {  
  97.                 response.close();  
  98.             }  
  99.         } catch (ClientProtocolException e) {  
  100.             e.printStackTrace();  
  101.         } catch (UnsupportedEncodingException e1) {  
  102.             e1.printStackTrace();  
  103.         } catch (IOException e) {  
  104.             e.printStackTrace();  
  105.         } finally {  
  106.             //Close the connection and release resources  
  107.             try {  
  108.                 httpclient.close();  
  109.             } catch (IOException e) {  
  110.                 e.printStackTrace();  
  111.             }  
  112.         }  
  113.     }  
  114.   
  115.     /** 
  116.      * Send a post request to access the local application and return different results depending on the passing parameters 
  117.      */  
  118.     public void post() {  
  119.         //Create a default httpClient instance.  
  120.         CloseableHttpClient httpclient = HttpClients.createDefault();  
  121.         //Create httppost  
  122.         HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceJ.action");  
  123.         //Create parameter queues  
  124.         List<namevaluepair> formparams = new ArrayList<namevaluepair>();  
  125.         formparams.add(new BasicNameValuePair("type""house"));  
  126.         UrlEncodedFormEntity uefEntity;  
  127.         try {  
  128.             uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");  
  129.             httppost.setEntity(uefEntity);  
  130.             System.out.println("executing request " + httppost.getURI());  
  131.             CloseableHttpResponse response = httpclient.execute(httppost);  
  132.             try {  
  133.                 HttpEntity entity = response.getEntity();  
  134.                 if (entity != null) {  
  135.                     System.out.println("--------------------------------------");  
  136.                     System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));  
  137.                     System.out.println("--------------------------------------");  
  138.                 }  
  139.             } finally {  
  140.                 response.close();  
  141.             }  
  142.         } catch (ClientProtocolException e) {  
  143.             e.printStackTrace();  
  144.         } catch (UnsupportedEncodingException e1) {  
  145.             e1.printStackTrace();  
  146.         } catch (IOException e) {  
  147.             e.printStackTrace();  
  148.         } finally {  
  149.             //Close the connection and release resources.  
  150.             try {  
  151.                 httpclient.close();  
  152.             } catch (IOException e) {  
  153.                 e.printStackTrace();  
  154.             }  
  155.         }  
  156.     }  
  157.   
  158.     /** 
  159.      * Send get request 
  160.      */  
  161.     public void get() {  
  162.         CloseableHttpClient httpclient = HttpClients.createDefault();  
  163.         try {  
  164.             //Create httpget.  
  165.             HttpGet httpget = new HttpGet("http://www.baidu.com/");  
  166.             System.out.println("executing request " + httpget.getURI());  
  167.             //Execute get requests.  
  168.             CloseableHttpResponse response = httpclient.execute(httpget);  
  169.             try {  
  170.                 //Getting response entities  
  171.                 HttpEntity entity = response.getEntity();  
  172.                 System.out.println("--------------------------------------");  
  173.                 //Print response status  
  174.                 System.out.println(response.getStatusLine());  
  175.                 if (entity != null) {  
  176.                     //Print response content length  
  177.                     System.out.println("Response content length: " + entity.getContentLength());  
  178.                     //Print response content  
  179.                     System.out.println("Response content: " + EntityUtils.toString(entity));  
  180.                 }  
  181.                 System.out.println("------------------------------------");  
  182.             } finally {  
  183.                 response.close();  
  184.             }  
  185.         } catch (ClientProtocolException e) {  
  186.             e.printStackTrace();  
  187.         } catch (ParseException e) {  
  188.             e.printStackTrace();  
  189.         } catch (IOException e) {  
  190.             e.printStackTrace();  
  191.         } finally {  
  192.             //Close the connection and release resources.  
  193.             try {  
  194.                 httpclient.close();  
  195.             } catch (IOException e) {  
  196.                 e.printStackTrace();  
  197.             }  
  198.         }  
  199.     }  
  200.   
  201.     /** 
  202.      * Upload files 
  203.      */  
  204.     public void upload() {  
  205.         CloseableHttpClient httpclient = HttpClients.createDefault();  
  206.         try {  
  207.             HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceFile.action");  
  208.   
  209.             FileBody bin = new FileBody(new File("F:\\image\\sendpix0.jpg"));  
  210.             StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);  
  211.   
  212.             HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("bin", bin).addPart("comment", comment).build();  
  213.   
  214.             httppost.setEntity(reqEntity);  
  215.   
  216.             System.out.println("executing request " + httppost.getRequestLine());  
  217.             CloseableHttpResponse response = httpclient.execute(httppost);  
  218.             try {  
  219.                 System.out.println("----------------------------------------");  
  220.                 System.out.println(response.getStatusLine());  
  221.                 HttpEntity resEntity = response.getEntity();  
  222.                 if (resEntity != null) {  
  223.                     System.out.println("Response content length: " + resEntity.getContentLength());  
  224.                 }  
  225.                 EntityUtils.consume(resEntity);  
  226.             } finally {  
  227.                 response.close();  
  228.             }  
  229.         } catch (ClientProtocolException e) {  
  230.             e.printStackTrace();  
  231.         } catch (IOException e) {  
  232.             e.printStackTrace();  
  233.         } finally {  
  234.             try {  
  235.                 httpclient.close();  
  236.             } catch (IOException e) {  
  237.                 e.printStackTrace();  
  238.             }  
  239.         }  
  240.     }  
  241. }</namevaluepair></namevaluepair></namevaluepair></namevaluepair>  

This example uses the latest version of HttpClient 4.3. This version is quite different from the previous code style, we should pay more attention to it.


That's the same sentence... Good things to reproduce, learn one can not be less, below Turn from here

HttpClient has several points to pay attention to in its application:

Where HttpClient is generated:

   ttpClient=new HttpClient();
  httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

First, you must have the following two parameter settings, otherwise you will generate multiple Cookie header s to the web server
  httpClient.getParams().setParameter("http.protocol.single-cookie-header",true);
  httpClient.getParams().setParameter("http.protocol.content-charset","gb2312");
  ArrayList headerList=new ArrayList();
  Header accept=new Header("Accept","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-silverlight, */*");
  headerList.add(accept);
    ...... 

  httpClient.getParams().setParameter("http.default-headers",headerList);
  httpClient.getParams().setParameter("http.protocol.version",HttpVersion.HTTP_1_1);
  httpClient.getParams().setParameter("http.method.retry-handler",new DefaultHttpMethodRetryHandler());

   

Second: Where httpMethod is generated:

     PostMethod postMethod =new PostMethod();
  postMethod.getParams().setUriCharset("GB2312");

     postMethod.setURI(new URI(url,false,"GB2312"));

     client.executeMethod(postMethod); 

Above can avoid sending something like "HTTP://127.0.0.1:8080/action.do?" The URI error occurred when the content = 123456 "URL.

Third: The Connection Pool Timeout Exception problem may be caused by several reasons:

1. Setting connection timeout time is not set or too small;

2. Whether the connection after request has been released;

For more information, as well as parameter settings and code writing, risks, see This article

Tags: Apache socket less SSL

Posted on Thu, 28 Mar 2019 14:39:29 -0400 by digitus78