HTTP is probably the most widely used and important protocol on the Internet. More and more Java applications need to access network resources directly through HTTP. Although the basic functionality to access the HTTP protocol is already provided in the Java NET package of the JDK, the JDK libraries themselves are not rich or flexible enough for most applications. HttpClient is a subproject of Apache Jakarta Common that provides an efficient, up-to-date, feature-rich client programming toolkit that supports the latest versions and recommendations of the HTTP protocol.

HTTP is like a browser, but it’s not a browser. Many people think that since HttpClient is an HTTP client programming tool, many people understand it as a browser. However, HttpClient is not a browser. It is an HTTP communication library, so it provides only a subset of the functionality expected by a generic browser application. The fundamental difference is that there is no user interface in HttpClient, the browser needs a rendering engine to display the page and interpret user input, such as a mouse click somewhere on the display page, and a layout engine that calculates how to display the HTML page, including cascading style sheets and images. The javascript interpreter runs javascript code embedded in or referenced from an HTML page. Events from the user interface are passed to the javascript interpreter for processing. In addition, there are interfaces for plug-ins that can work with applets, embedded media objects (such as PDF files, Quicktime movies, and Flash animations) or ActiveX controls that can perform any operation. HttpClient can only be used programmatically through its API to transport and receive HTTP messages.

HttpClient provides the following functions:

Implement all HTTP methods (GET, POST, PUT, HEAD, DELETE, HEAD, OPTIONS, etc.) support HTTPS protocol support proxy server (Nginx, etc.) support automatic (jump) turn

Get into the business

Environment description: JDK1.8, SpringBoot

Preparation Step 1: Introduce the HttpClient dependency in POM.xml

Step 2: Introduce fastJSON dependencies

Note: I introduced this dependency so that the “ability to convert objects to JSON strings” will be used in future examples, as well as other dependencies that have this capability.

Note: SpringBoot’s basic dependency configuration is not covered here.

In this example, HttpClient is sent in JAVA (sent by unit test in test); Also received in JAVA (received in Controller).

Declaration: the following code, I test effective.

GET No parameter: HttpClient send example:

@date 2018年7月13日 4:18:50 */ @test public void doGetTestOne() { CloseableHttpClient HttpClient = httpClientBuilder.create ().build(); / / create a Get request HttpGet HttpGet = new HttpGet (” http://localhost:12345/doGetControllerOne “); // CloseableHttpResponse Response = null; Response = httpClient.execute(httpGet); // Get responseEntity responseEntity = response.getentity (); System.out.println(” Response status :” + response.getStatusLine()); if (responseEntity ! = null) {System. Out. Println (” response content length is: “+ responseEntity. GetContentLength ()); System.out.println(” entityutils.toString (responseEntity) “); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally {try {// Release resources if (httpClient! = null) { httpClient.close(); } if (response ! = null) { response.close(); } } catch (IOException e) { e.printStackTrace(); }}} Receive example:

GET with parameters (Method 1: Directly concatenate the URL) : Example of sending an HttpClient:

** @date 2018年7月13日 4:19:23 */ @test public void doGetTestWayOne() {// Get an Http client (you need a browser; CloseableHttpClient HttpClient = httpClientBuilder.create ().build(); Params = new StringBuffer(); Try {// Character data should be below encoding; Params.append (“name=” + URLEncoder. Encode (“&”, “utF-8 “)); params.append(“&”); params.append(“age=24″); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } / / create a Get request HttpGet HttpGet = new HttpGet (” http://localhost:12345/doGetControllerTwo “+”?” + params); // CloseableHttpResponse Response = null; RequestConfig = requestConfig.custom () // Set connection timeout in milliseconds. SetConnectTimeout (5000) // Set the request timeout (in milliseconds). SetConnectionRequestTimeout (5000) / / socket read and write timeout (in milliseconds) setSocketTimeout (5000) / / set whether to allow the redirection (the default is true) .setRedirectsEnabled(true).build(); // apply the above configuration information to the Get request httpget.setConfig (requestConfig); // The client executes (sends) the Get request response = httpClient.execute(httpGet); // Get responseEntity responseEntity = response.getentity (); System.out.println(” Response status :” + response.getStatusLine()); if (responseEntity ! = null) {System. Out. Println (” response content length is: “+ responseEntity. GetContentLength ()); System.out.println(” entityutils.toString (responseEntity) “); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally {try {// Release resources if (httpClient! = null) { httpClient.close(); } if (response ! = null) { response.close(); } } catch (IOException e) { e.printStackTrace(); }}} Receive example:

GET with parameters (Method 2: use the URI to GET HttpGet) :

** @date 2018年7月13日 4:19:23 */ @test public void */ @test public void ** @date 2018年7月13日 4:19:23 */ @test public void DoGetTestWayTwo () {// Get the Http client. CloseableHttpClient HttpClient = httpClientBuilder.create ().build(); // Parameter URI URI = null; List

params = new ArrayList<>(); params.add(new BasicNameValuePair(“name”, “&”)); params.add(new BasicNameValuePair(“age”, “18”)); // Set the URI information and put the parameter set into the URI; // setParameter(String key, String value) uri = new URIBuilder().setScheme(“http”).setHost(“localhost”) .setPort(12345).setPath(“/doGetControllerTwo”) .setParameters(params).build(); } catch (URISyntaxException e1) { e1.printStackTrace(); } // create a request HttpGet HttpGet = new HttpGet(uri); // CloseableHttpResponse Response = null; RequestConfig = requestConfig.custom () // Set connection timeout in milliseconds. SetConnectTimeout (5000) // Set the request timeout (in milliseconds). SetConnectionRequestTimeout (5000) / / socket read and write timeout (in milliseconds) setSocketTimeout (5000) / / set whether to allow the redirection (the default is true) .setRedirectsEnabled(true).build(); // apply the above configuration information to the Get request httpget.setConfig (requestConfig); // The client executes (sends) the Get request response = httpClient.execute(httpGet); // Get responseEntity responseEntity = response.getentity (); System.out.println(” Response status :” + response.getStatusLine()); if (responseEntity ! = null) {System. Out. Println (” response content length is: “+ responseEntity. GetContentLength ()); System.out.println(” entityutils.toString (responseEntity) “); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally {try {// Release resources if (httpClient! = null) { httpClient.close(); } if (response ! = null) { response.close(); } } catch (IOException e) { e.printStackTrace(); }}} Receive example:

POST No parameter: HttpClient Sending example:

/** * POST– No Test ** @date 2018年7月13日 4:18:50 */ @test public void doPostTestOne() { CloseableHttpClient HttpClient = httpClientBuilder.create ().build(); / / create a Post request HttpPost HttpPost = new HttpPost (” http://localhost:12345/doPostControllerOne “); // CloseableHttpResponse Response = null; Response = httpClient.execute(httpPost); // Get responseEntity responseEntity = response.getentity (); System.out.println(” Response status :” + response.getStatusLine()); if (responseEntity ! = null) {System. Out. Println (” response content length is: “+ responseEntity. GetContentLength ()); System.out.println(” entityutils.toString (responseEntity) “); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally {try {// Release resources if (httpClient! = null) { httpClient.close(); } if (response ! = null) { response.close(); } } catch (IOException e) { e.printStackTrace(); }}} Receive example:

POST with parameters (common parameters) : Note: Common parameters can be passed through POST in the same way as GET. In this example, parameters are directly appended to the URL suffix.

Examples of HttpClient sending:

** @date 2018年7月13日 4:18:50 */ @test public void doPostTestFour() {// Get an Http client (you need a browser; CloseableHttpClient HttpClient = httpClientBuilder.create ().build(); Params = new StringBuffer(); Try {// Character data should be below encoding; Params.append (“name=” + URLEncoder. Encode (“&”, “utF-8 “)); params.append(“&”); params.append(“age=24″); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } / / create a Post request HttpPost HttpPost = new HttpPost (” http://localhost:12345/doPostControllerFour “+”?” + params); Httppost.setheader (” content-type “, “application/json; charset=utf8″); // CloseableHttpResponse Response = null; Response = httpClient.execute(httpPost); // Get responseEntity responseEntity = response.getentity (); System.out.println(” Response status :” + response.getStatusLine()); if (responseEntity ! = null) {System. Out. Println (” response content length is: “+ responseEntity. GetContentLength ()); System.out.println(” entityutils.toString (responseEntity) “); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally {try {// Release resources if (httpClient! = null) { httpClient.close(); } if (response ! = null) { response.close(); } } catch (IOException e) { e.printStackTrace(); }}} Receive example:

POST with arguments (object arguments) : The User class is given first

Examples of HttpClient sending:

** @date 2018年7月13日 4:18:50 */ @test public void doPostTestTwo() {// Get an Http client (you need a browser; CloseableHttpClient HttpClient = httpClientBuilder.create ().build(); / / create a Post request HttpPost HttpPost = new HttpPost (” http://localhost:12345/doPostControllerTwo “); User user = new User(); User.setname (” pan Xiaoting “); user.setAge(18); User. SetGender (” female “); User. SetMotto (” Be graceful ~”); // I use Ali’s fastjson to convert Object to JSON string; / / (you need to import the com. Alibaba. Fastjson. JSON packet) String jsonString = JSON. ToJSONString (user); StringEntity entity = new StringEntity(jsonString, “UTF-8”); // Post requests pass parameters in the request body; This puts entity in the body of the POST request httpPost.setentity (entity); httpPost.setHeader(“Content-Type”, “application/json; charset=utf8″); // CloseableHttpResponse Response = null; Response = httpClient.execute(httpPost); // Get responseEntity responseEntity = response.getentity (); System.out.println(” Response status :” + response.getStatusLine()); if (responseEntity ! = null) {System. Out. Println (” response content length is: “+ responseEntity. GetContentLength ()); System.out.println(” entityutils.toString (responseEntity) “); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally {try {// Release resources if (httpClient! = null) { httpClient.close(); } if (response ! = null) { response.close(); } } catch (IOException e) { e.printStackTrace(); }}} Receive example:

POST has parameters (common parameters + object parameters) : Note: The method used to pass common parameters through POST is the same as that used to pass common parameters through GET. In this example, HttpPost is obtained through the URI.

First give the User class:

Examples of HttpClient sending:

** @date 2018年7月13日 4:18:50 */ @test public void doPostTestThree() {// Get an Http client (you need a browser; CloseableHttpClient HttpClient = httpClientBuilder.create ().build(); // Create a Post request // parameter URI URI = null; List

params = new ArrayList<>(); params.add(new BasicNameValuePair(“flag”, “4”)); Params. add(new BasicNameValuePair(” Meaning “, “What is this?”) )); // Set the URI information and put the parameter set into the URI; // setParameter(String key, String value) uri = new URIBuilder().setScheme(“http”).setHost(“localhost”).setPort(12345) .setPath(“/doPostControllerThree”).setParameters(params).build(); } catch (URISyntaxException e1) { e1.printStackTrace(); } HttpPost httpPost = new HttpPost(uri); // HttpPost httpPost = new // HttpPost(“http://localhost:12345/doPostControllerThree1″); // create user parameter user user = new user (); User.setname (” pan Xiaoting “); user.setAge(18); User. SetGender (” female “); User. SetMotto (” Be graceful ~”); StringEntity Entity = new StringEntity(json.tojsonString (user), “utF-8 “); // Post requests pass parameters in the request body; This puts entity in the body of the POST request httpPost.setentity (entity); httpPost.setHeader(“Content-Type”, “application/json; charset=utf8″); // CloseableHttpResponse Response = null; Response = httpClient.execute(httpPost); // Get responseEntity responseEntity = response.getentity (); System.out.println(” Response status :” + response.getStatusLine()); if (responseEntity ! = null) {System. Out. Println (” response content length is: “+ responseEntity. GetContentLength ()); System.out.println(” entityutils.toString (responseEntity) “); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally {try {// Release resources if (httpClient! = null) { httpClient.close(); } if (response ! = null) { response.close(); } } catch (IOException e) { e.printStackTrace(); }}} Receive example:

Note: If you want to know the complete code and test details, go to the project hosting link below and clone the project. If you need to run tests, you can start the SpringBoot project and then run the relevant test method to test it.

Resolve garbled response issues (example) :

Making an HTTPS request with (or without) certificate verification (example) :

Method details (non-perfect encapsulation) :

/** * Get the HttpClient based on whether it is an HTTPS request. For the choice of calibration certificate, I here is written dead * in the code, when you use, you can be flexible secondary encapsulation. * * Tip: For encapsulation of this tool class, and generation of related client and server certificates, refer to my blog: * < Linked >blog.csdn.net/justry_deng… > * * * @param isHttps whether HTTPS request * * @return HttpClient instance * @date 2019/9/18 17:57 */ private CloseableHttpClient getHttpClient(boolean isHttps) { CloseableHttpClient httpClient; if (isHttps) { SSLConnectionSocketFactory sslSocketFactory; SslSocketFactory = getSocketFactory(false, null, null); sslSocketFactory = getSocketFactory(false, null, null); sslSocketFactory = getSocketFactory(false, null, null); InputStream ca = this.getClass().getClassLoader().getResourceasStream (“client/ds.crt”); // Alias of the certificate, namely :key. Note :cAalias only needs to be unique, but the alias used to generate the keystore is recommended. // String cAalias = System.currentTimeMillis() + “” + new SecureRandom().nextInt(1000); //sslSocketFactory = getSocketFactory(true, ca, cAalias); } catch (Exception e) { throw new RuntimeException(e); } httpClient = HttpClientBuilder.create().setSSLSocketFactory(sslSocketFactory).build(); return httpClient; } httpClient = HttpClientBuilder.create().build(); return httpClient; } /** * HTTPS helper method, Create SSLSocketFactory instance, TrustManager instance for HTTPS requests * * @param needVerifyCa * Whether the CA certificate needs to be verified (that is, whether the server needs to be verified) * @param caInputStream * CA certificate. * @param cAalias * Alias. * Note: aliases should be unique. They should not be the same as other aliases, otherwise they will overwrite the certificate information of previous aliases. An alias is a key in a key-value. * * * @ return SSLConnectionSocketFactory instance @ throws exception information NoSuchAlgorithmException * * @ throws CertificateException * exception information * @throws KeyStoreException * Exception information * @throws IOException * Exception information * @throws KeyManagementException * Exception information * @date 2019/6/11 19:52 */ private static SSLConnectionSocketFactory getSocketFactory(boolean needVerifyCa, InputStream caInputStream, String cAalias) throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, KeyManagementException { X509TrustManager x509TrustManager; KeyStore KeyStore = getKeyStore(caInputStream, cAalias); if (needVerifyCa) {KeyStore KeyStore = getKeyStore(caInputStream, cAalias); TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(keyStore); TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); if (trustManagers.length ! = 1 | |! (trustManagers[0] instanceof X509TrustManager)) { throw new IllegalStateException(“Unexpected default trust managers:” + Arrays.toString(trustManagers)); } x509TrustManager = (X509TrustManager) trustManagers[0]; SSLContext SSLContext = sslContext. getInstance(“TLS”); sslContext.init(null, new TrustManager[]{x509TrustManager}, new SecureRandom()); return new SSLConnectionSocketFactory(sslContext); } // HTTPS request, X509TrustManager = new x509TrustManager () {@override public void checkClientTrusted(X509Certificate[] arg0, String arg1) { } @Override public void checkServerTrusted(X509Certificate[] arg0, String arg1) {// Not validate} @override public X509Certificate[] getAcceptedIssuers() {return new X509Certificate[0]; }}; SSLContext sslContext = SSLContext.getInstance(“TLS”); sslContext.init(null, new TrustManager[]{x509TrustManager}, new SecureRandom()); return new SSLConnectionSocketFactory(sslContext); Note: This repository is used to store keys and certificates ** @param caInputStream * CA certificate (which should be provided by the server to be accessed) * @param cAalias * alias * Note: Aliases should be unique. Aliases should not be the same as other aliases, otherwise they will overwrite the certificate information of previous aliases. An alias is a key in a key-value. * @return Key and certificate store * @throws KeyStoreException exception * @throws CertificateException exception information * @throws IOException exception information * @throws NoSuchAlgorithmException Exception information * @date 2019/6/11 18:48 */ private static KeyStore getKeyStore(InputStream caInputStream, String cAalias) throws KeyStoreException, CertificateException, IOException, NoSuchAlgorithmException {/ / certificate factory CertificateFactory CertificateFactory = CertificateFactory. GetInstance (” X. 509 “); KeyStore KeyStore = keystore.getInstance (keystore.getDefaultType ()); // KeyStore KeyStore = keystore.getDefaultType (); keyStore.load(null); keyStore.setCertificateEntry(cAalias, certificateFactory.generateCertificate(caInputStream)); return keyStore; } Application /x-www-form-urlencoded form requests (example) :

Sending files (example) : Preparations:

Convenient if you want to transfer files, in addition to the introduction of org. Apache. Httpcomponents introduced basic httpclient relying on an extra org.. Apache httpcomponents httpmime dependence. P.S. : you can transfer files without introducing httpmime dependencies, but it’s not powerful enough.

Additional introduction in POM.xml:

< the dependency > < groupId > org.. Apache httpcomponents < / groupId > < artifactId > httpmime < / artifactId >

4.5.5
The sender looks like this:

/** ** send file ** multipart/form-data Send file (and related information) ** Note: If you want to transfer files flexibly and conveniently, . * in addition to the introduction of org. Apache httpcomponents basic httpclient rely on outside * to introduce additional org.. Apache httpcomponents httpmime dependence. * Note: It is possible to transfer files without introducing httpMIME dependencies, but it is not powerful enough. * */ @Test public void test4() { CloseableHttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost(“http://localhost:12345/file”); CloseableHttpResponse response = null; try { MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); String filesKey = “files”; File file1 = new File(“C:\\Users\\JustryDeng\\Desktop\\back.jpg”); multipartEntityBuilder.addBinaryBody(filesKey, file1); // File file2 = new File(“C:\ Users\\JustryDeng\\Desktop\ avature.jpg “); // Prevent garbled file names received by the server. Here we can first put the file name URLEncode, and then the server gets the file name in URLDecode. You can avoid the garble problem. // The file name is actually transmitted in the content-disposition of the request header, as in form-data; name=”files”; Filename = “avatar. JPG” multipartEntityBuilder addBinaryBody (filesKey, file2, ContentType DEFAULT_BINARY, URLEncoder.encode(file2.getName(), “utf-8”)); // Other parameters (note: custom contentType, Create (“text/plain”, charset.forname (” utF-8 “)) ContentType ContentType = contentType. create(“text/plain”, charset.forname (” utF-8 “)); MultipartEntityBuilder. AddTextBody (” name “, “deng” Sullivan, contentType); multipartEntityBuilder.addTextBody(“age”, “25”, contentType); HttpEntity httpEntity = multipartEntityBuilder.build(); httpPost.setEntity(httpEntity); response = httpClient.execute(httpPost); HttpEntity responseEntity = response.getEntity(); System.out.println(“HTTPS response status :” + response.getStatusLine()); if (responseEntity ! = null) {System. Out. Println (” HTTPS response content length is: “+ responseEntity. GetContentLength ()); String responseStr = entityutils.tostring (responseEntity, standardCharsets.utf_8); System.out.println(“HTTPS response :” + responseStr); } } catch (ParseException | IOException e) { e.printStackTrace(); } finally {try {// Release resources if (httpClient! = null) { httpClient.close(); } if (response ! = null) { response.close(); } } catch (IOException e) { e.printStackTrace(); }}} The receiver looks like this:

Sending stream (example) : The sending side looks like this:

/** ** ** / @test public void test5() {CloseableHttpClient httpClient = httpClientBuilder.create ().build(); HttpPost httpPost = new HttpPost(“http://localhost:12345/is? Name = deng Sullivan “); CloseableHttpResponse response = null; Try {InputStream is = new ByteArrayInputStream(” Stream ~”.getBytes()); InputStreamEntity ise = new InputStreamEntity(is); httpPost.setEntity(ise); response = httpClient.execute(httpPost); HttpEntity responseEntity = response.getEntity(); System.out.println(“HTTPS response status :” + response.getStatusLine()); if (responseEntity ! = null) {System. Out. Println (” HTTPS response content length is: “+ responseEntity. GetContentLength ()); String responseStr = entityutils.tostring (responseEntity, standardCharsets.utf_8); System.out.println(“HTTPS response :” + responseStr); } } catch (ParseException | IOException e) { e.printStackTrace(); } finally {try {// Release resources if (httpClient! = null) { httpClient.close(); } if (response ! = null) { response.close(); } } catch (IOException e) { e.printStackTrace(); }}} The receiver looks like this:

 

Again: if you want to test yourself, you can go to the project hosting link given below, clone the project, and then start the SpringBoot project, and then run the relevant test method, test.

Utility class tip: When using HttpClient, write it as a utility class as appropriate. For example, one of the most popular HttpClient utility classes on Github is httpClientUtil. I also recommend using this tool class here, because the writer of this tool class encapsulates a lot of functionality in it, and if you don’t have any special needs, you can use this tool class without building wheels. Use method is simple, can see the https://github.com/Arronlong/httpclientutil.