Java implements the HTTP request utility class example

  • 2020-04-01 03:17:57
  • OfStack

The data is returned via an HTTP rest request


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class HttpClientUtils {
    private static final Log log = LogFactory.getLog(HttpClientUtils.class);
    
    private static HttpClient httpClient = null;
    
    public static HttpClient getHttpClient() {
        if (httpClient == null) {
            httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
        }
        return httpClient;
    }
    
    public static String executeByPOST(String url, List<NameValuePair> params) {
        HttpClient httpclient = getHttpClient();
        HttpPost post = new HttpPost(url);

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseJson = null;
        try {
            if (params != null) {
                post.setEntity(new UrlEncodedFormEntity(params));
            }
            responseJson = httpclient.execute(post, responseHandler);
            log.info("HttpClient POST Result of request: " + responseJson);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            log.info("HttpClient POST Request exception: " + e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            httpclient.getConnectionManager().closeExpiredConnections();
            httpclient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);
        }
        return responseJson;
    }
    /**
     * Get Way to request 
     *
     * @param url     With an argument placeholder URL Example: http:/User/user/center.aspx?_action=GetSimpleUserInfo&codes={0}&email={1}
     * @param params  Array of parameter values, with url The placeholder sequence corresponds 
     * @return  Response string 
     * @throws java.io.UnsupportedEncodingException
     */
    public static String executeByGET(String url, Object[] params) {
        HttpClient httpclient = getHttpClient();
        String messages = MessageFormat.format(url, params);
        HttpGet get = new HttpGet(messages);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseJson = null;
        try {
            responseJson = httpclient.execute(get, responseHandler);
            log.info("HttpClient GET Result of request: " + responseJson);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            log.info("HttpClient GET Request exception: " + e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            log.info("HttpClient GET Request exception: " + e.getMessage());
        } finally {
            httpclient.getConnectionManager().closeExpiredConnections();
            httpclient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);
        }
        return responseJson;
    }
    
    public static String executeByGET(String url) {
        HttpClient httpclient = getHttpClient();
        HttpGet get = new HttpGet(url);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseJson = null;
        try {
            responseJson = httpclient.execute(get, responseHandler);
            log.info("HttpClient GET Result of request: " + responseJson);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            log.info("HttpClient GET Request exception: " + e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            log.info("HttpClient GET Request exception: " + e.getMessage());
        } finally {
            httpclient.getConnectionManager().closeExpiredConnections();
            httpclient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);
        }
        return responseJson;
    }
}


Related articles: