Access https interface using Apache HttpComponents (and old DefaultHttpClient deprecated)

Keywords: Apache JSON Maven xml

Preface

Recently, the company has made a group of interfaces encrypted by https protocol certificate. In order to make it easier for users to use this interface, it has made an example program of access. This article records this example program.

 

DefaultHttpClient deprecated

Access to https uses the Apache HttpComponents tool, and the DefaultHttpClient class is used to create requests in some earlier versions of the Web. But since version 4.3, the DefaultHttpClient class is no longer recommended, so this example uses the CloseableHttpClient class to create requests.

Official API description of DefaultHttpClient

Deprecated. 
(4.3) use HttpClientBuilder see also CloseableHttpClient.

 

Example

Simple program to code directly

maven introducing package

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

Test code

package pub.lichao.test.controller;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

/**
 * post request with HttpClient
 */
public class HttpClientUtil {
    /**
     * Send a post request
     * @param url url of resources
     * @param jsonstr json Format input
     * @param charset Coding mode
     * @return
     */
    public static String doPost(String url,String jsonstr,String charset){
        String result = null;
        try{
            //Create CloseableHttpClient class
            HttpClient httpClient =  HttpClientBuilder.create().build();
            //Create post request class
            HttpPost httpPost = new HttpPost(url);
            //Increase http header information to support json
            httpPost.addHeader("Content-Type", "application/json");
            StringEntity se = new StringEntity(jsonstr);
            se.setContentType("text/json");
            se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
            httpPost.setEntity(se);
            //Create response requests
            HttpResponse response = httpClient.execute(httpPost);
            if(response != null){
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){
                    //The result is converted to string type
                    result = EntityUtils.toString(resEntity,charset);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * test method
     * @param args
     */
    public static void main(String[] args){
        String url = "https://openapi.puliantongxun.com/v1/get_token";
        String jsonStr = "{\"appId\":\"username\",\"appSecret\":\"secret\"}";
        String httpOrgCreateTestRtn = doPost(url, jsonStr, "utf-8");
        System.out.println("https result is :" + httpOrgCreateTestRtn);
    }
}

Posted by SQL Maestro on Tue, 12 Feb 2019 05:51:17 -0800