Httpclient tools (get, put)

package com.googosoft.until;

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
Import org.slf4j.Logger;
 Import org.slf4j.LoggerFactory;
 Import com.alibaba.fastjson.JSONArray;
 Import com.googosoft.model.HttpClientResult; 

/ **  
* @author Songyan 
* @version 1 2020 January 13 pm : 57 is: 15 
* @desc HttpClient tools send http requests 
* / 
public  class HttpClientUtil { 

    protected  static  Final Logger Logger = LoggerFactory.getLogger (HttpClientUtil. class ); 

    / ** 
     * get request transmitted without parameters 
     * @param URL 
     * @return 
     *@throws ClientProtocolException
     * @throws IOException
     */
    public static HttpClientResult sendGetRequest(String url){
        HttpClientResult result = new HttpClientResult();
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet get = new HttpGet(url);
        CloseableHttpResponse response;
        try {
            response = client.execute(get);
            HttpEntity entity = response.getEntity();
            String content = EntityUtils.toString (Entity); 
            result.setContent (Content); 
            result.setCode (response.getStatusLine () getStatusCode ().); 
        } The catch (ClientProtocolException E) { 
            logger.error ( "protocol anomaly, as the stack information," , E); 
        } the catch (IOException E) { 
            logger.error ( "network anomalies, as stack information" , E); 
        } the finally {
             the try { 
                client.close (); 
            } the catch (exception E) { 
                e.printStackTrace (); 
                Client = null;
            }
        }
        return result;
    }

    /**
     * 发送无参put请求
     * @param url
     * @return
     */
    public static HttpClientResult sendPutReq(String url) {
        HttpClientResult result = new HttpClientResult();
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPut put = new HttpPut(url);
        try {
            HttpResponse httpResponse = httpClient.execute(put);
            IF (httpResponse.getStatusLine () getStatusCode () == 200 is. ) { 
                the HttpEntity Entity = httpResponse.getEntity (); 
                String Content = EntityUtils.toString (Entity); 
                result.setContent (Content); 
            } the else { 
                result.setCode (the httpResponse .getStatusLine () getStatusCode ());. 
            } 
        } the catch (ClientProtocolException E) { 
            logger.error ( "protocol anomaly, as stack information" , E); 
        } the catch (IOException E) { 
            logger.error ( "network anomalies stack Message as follows" , E);
        } finally {
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
                httpClient = null;
            }
        }
        return result;
    }

    public static JSONArray StrToJsonArray(String content) {
        
        return null;
    }
    
}

Guess you like

Origin www.cnblogs.com/excellencesy/p/12214205.html