Project Summary 38: Use httpcomponents: httpclient request data

Project Summary 38: Use httpcomponents: httpclient request data

 

Maven relies

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.6</version>
        </dependency>

The sample code

package com.hzsun.shr.web.maintenance.util.http;

/* *
 *@Description:
 *@Author:TYJ
 *@Date: create in  2019/10/29 14:56
 */

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpClientImplTemp {

    /**
     *@Description 1-POST请求
     *@param  //blog:https://www.cnblogs.com/hhhshct/p/8523697.html
     *@return  java.lang.String
     *@author  TangYujie
     *@date  2019/10/29 15:34
     */
    public static String doPostWithHeaderAndJsonBody(String url, Map<String, Object> headerParamMap,String jsonBody) {
        HttpClient CloseableHttpClient = null ; 
        CloseableHttpResponse the httpResponse = null ; 
        String Result = "" ;
         // Create instance httpClient 
        httpClient = HttpClients.createDefault ();
         // Create a remote connection HttpPost example 
        HttpPost HttpPost = new new HttpPost (URL);
         // configuration request parameter examples 
        requestConfig requestConfig RequestConfig.custom = (). setConnectTimeout (35000) // set the hosting service connection timeout 
                .setConnectionRequestTimeout (35000) // set the connection request time
                .setSocketTimeout (60000) // set the read data connection timeout 
                .build ();
         // set configured httpPost example 
        httpPost.setConfig (requestConfig);
         // Set request header
         // httpPost.addHeader ( "the Type-the Content", "file application / X-WWW-form-urlencoded"); 
        httpPost.addHeader ( "the Content-the Type", "file application / JSON" ); // set according to the actual situation of 
        the set <String> headerKeys = headerParamMap.keySet ();
         for ( Key String: headerKeys) { 
            httpPost.addHeader (Key, headerParamMap.get (Key) .toString ()); 
        } 
        // package body parameter request 
        httpPost.setEntity(new StringEntity (jsonBody, "UTF-. 8" ));
        try{
             // httpClient post requests on the object, the object and returns a response parameter 
            the httpResponse = httpClient.execute (HttpPost);
             // Get the contents of the response from the response object 
            the HttpEntity Entity = httpResponse.getEntity (); 
            Result = EntityUtils.toString (Entity); 
            System.out.println ( "Result:" + Result); 
        } the catch (ClientProtocolException E) { 
            e.printStackTrace (); 
        } the catch (IOException E) { 
            e.printStackTrace (); 
        } the finally {
             // close the resource 
            if (null != httpResponse) {
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}

 

Guess you like

Origin www.cnblogs.com/wobuchifanqie/p/11765006.html