httputil工具類


import com.alibaba.fastjson.JSONObject;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
 
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
 
/**
 * http請求工具類
 *
 * @author liujiong
 */
public class HttpUtil {
 
    private Logger logger = LoggerFactory.getLogger(HttpUtil.class);
 
    private static PoolingHttpClientConnectionManager pcm;//httpclient連接池
    private CloseableHttpClient httpClient = null; //http連接
    private int connectTimeout = 120000;//連接超時時間
    private int connectionRequestTimeout = 10000;//從連接池獲取連接超時時間
    private int socketTimeout = 300000;//獲取數據超時時間
    private String charset = "utf-8";
    private RequestConfig requestConfig = null;//請求配置
    private Builder requestConfigBuilder = null;//build requestConfig
 
    private List<NameValuePair> nvps = new ArrayList<>();
    private List<Header> headers = new ArrayList<>();
    private String requestParam = "";
 
    static {
        pcm = new PoolingHttpClientConnectionManager();
        pcm.setMaxTotal(50);//整個連接池最大連接數
        pcm.setDefaultMaxPerRoute(50);//每路由最大連接數,默認值是2
    }
 
    /**
     * 默認設置
     *
     * @author Liu Jiong
     * @createDate 2016年10月30日
     */
    private static HttpUtil defaultInit() {
        HttpUtil httpUtil = new HttpUtil();
        if (httpUtil.requestConfig == null) {
            httpUtil.requestConfigBuilder = RequestConfig.custom().setConnectTimeout(httpUtil.connectTimeout)
                    .setConnectionRequestTimeout(httpUtil.connectionRequestTimeout)
                    .setSocketTimeout(httpUtil.socketTimeout);
            httpUtil.requestConfig = httpUtil.requestConfigBuilder.build();
        }
        return httpUtil;
    }
 
    /**
     * 初始化 httpUtil
     */
    public static HttpUtil init() {
        HttpUtil httpUtil = defaultInit();
        if (httpUtil.httpClient == null) {
            httpUtil.httpClient = HttpClients.custom().setConnectionManager(pcm).build();
        }
        return httpUtil;
    }
 
    /**
     * 初始化 httpUtil
     */
    public static HttpUtil init(Map<String, String> paramMap) {
        HttpUtil httpUtil = init();
        httpUtil.setParamMap(paramMap);
        return httpUtil;
    }
 
    /**
     * 驗證初始化
     */
    public static HttpUtil initWithAuth(String ip, int port, String username, String password) {
        HttpUtil httpUtil = defaultInit();
        if (httpUtil.httpClient == null) {
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(new AuthScope(ip, port, AuthScope.ANY_REALM), new UsernamePasswordCredentials(username, password));
            httpUtil.httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider)
                    .setConnectionManager(pcm).build();
        }
        return httpUtil;
    }
 
    /**
     * 設置請求頭
     */
    public HttpUtil setHeader(String name, String value) {
        Header header = new BasicHeader(name, value);
        headers.add(header);
        return this;
    }
 
    /**
     * 設置請求頭
     */
    public HttpUtil setHeaderMap(Map<String, String> headerMap) {
        for (Entry<String, String> param : headerMap.entrySet()) {
            Header header = new BasicHeader(param.getKey(), param.getValue());
            headers.add(header);
        }
        return this;
    }
 
    /**
     * 設置請求參數
     */
    public HttpUtil setParam(String name, String value) {
        nvps.add(new BasicNameValuePair(name, value));
        return this;
    }
 
    /**
     * 設置請求參數
     */
    public HttpUtil setParamMap(Map<String, String> paramMap) {
        for (Entry<String, String> param : paramMap.entrySet()) {
            nvps.add(new BasicNameValuePair(param.getKey(), param.getValue()));
        }
        return this;
    }
 
    /**
     * 設置字符串參數
     */
    public HttpUtil setStringParam(String requestParam) {
        this.requestParam = requestParam;
        return this;
    }
 
    /**
     * 設置連接超時時間
     */
    public HttpUtil setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
        this.requestConfigBuilder = requestConfigBuilder.setConnectTimeout(connectTimeout);
        requestConfig = requestConfigBuilder.build();
        return this;
    }
 
    /**
     * http get 請求
     */
    public Map<String, String> get(String url) {
        Map<String, String> resultMap = new HashMap<>();
        //獲取請求URI
        URI uri = getUri(url);
        if (uri != null) {
            HttpGet httpGet = new HttpGet(uri);
            httpGet.setConfig(requestConfig);
            if (!CollectionUtils.isEmpty(headers)) {
                Header[] header = new Header[headers.size()];
                httpGet.setHeaders(headers.toArray(header));
            }
 
            //執行get請求
            try {
                CloseableHttpResponse response = httpClient.execute(httpGet);
                return getHttpResult(response, url, httpGet, resultMap);
            } catch (Exception e) {
                httpGet.abort();
                resultMap.put("result", e.getMessage());
                logger.error("獲取http GET請求返回值失敗 url======" + url, e);
            }
        }
        return resultMap;
    }
 
    /**
     * http post 請求
     */
    public Map<String, String> post(String url) {
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(requestConfig);
        if (!CollectionUtils.isEmpty(headers)) {
            Header[] header = new Header[headers.size()];
            httpPost.setHeaders(headers.toArray(header));
        }
        if (!CollectionUtils.isEmpty(nvps)) {
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(nvps, charset));
            } catch (UnsupportedEncodingException e) {
                logger.error("http post entity form error", e);
            }
        }
        if (!StringUtils.isEmpty(requestParam)) {
            try {
                httpPost.setEntity(new StringEntity(requestParam, charset));
            } catch (UnsupportedCharsetException e) {
                logger.error("http post entity form error", e);
            }
        }
        Map<String, String> resultMap = new HashMap<>();
        //執行post請求
        try {
            CloseableHttpResponse response = httpClient.execute(httpPost);
            return getHttpResult(response, url, httpPost, resultMap);
        } catch (Exception e) {
            httpPost.abort();
            resultMap.put("result", e.getMessage());
            logger.error("獲取http POST請求返回值失敗 url======" + url, e);
        }
        return resultMap;
    }
 
    /**
     * post 上傳文件
     */
    public Map<String, String> postUploadFile(String url, Map<String, File> fileParam) {
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(requestConfig);
        if (!CollectionUtils.isEmpty(headers)) {
            Header[] header = new Header[headers.size()];
            httpPost.setHeaders(headers.toArray(header));
        }
 
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        if (fileParam != null) {
            for (Entry<String, File> entry : fileParam.entrySet()) {
                //將要上傳的文件轉化為文件流
                FileBody fileBody = new FileBody(entry.getValue());
                //設置請求參數
                builder.addPart(entry.getKey(), fileBody);
            }
        }
 
        if (!CollectionUtils.isEmpty(nvps)) {
            for (NameValuePair nvp : nvps) {
                String value = nvp.getValue();
                if (!StringUtils.isEmpty(value)) {
                    builder.addTextBody(nvp.getName(), value, ContentType.create("text/plain", charset));
                }
            }
        }
        httpPost.setEntity(builder.build());
        Map<String, String> resultMap = new HashMap<>();
        //執行post請求
        try {
            CloseableHttpResponse response = httpClient.execute(httpPost);
            return getHttpResult(response, url, httpPost, resultMap);
        } catch (Exception e) {
            httpPost.abort();
            resultMap.put("result", e.getMessage());
            logger.error("獲取http postUploadFile 請求返回值失敗 url======" + url, e);
        }
        return resultMap;
    }
 
    /**
     * 獲取請求返回值
     */
    private Map<String, String> getHttpResult(CloseableHttpResponse response, String url, HttpUriRequest request, Map<String, String> resultMap) {
        String result = "";
        int statusCode = response.getStatusLine().getStatusCode();
        resultMap.put("statusCode", statusCode + "");
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            try {
                result = EntityUtils.toString(entity, charset);
                EntityUtils.consume(entity);//釋放連接
            } catch (Exception e) {
                logger.error("獲取http請求返回值解析失敗", e);
                request.abort();
            }
        }
        if (statusCode != 200) {
            result = "HttpClient status code :" + statusCode + "  request url===" + url;
            logger.info("HttpClient status code :" + statusCode + "  request url===" + url);
            request.abort();
        }
        resultMap.put("result", result);
        return resultMap;
    }
 
    /**
     * 獲取重定向url返回的location
     */
    public String redirectLocation(String url) {
        String location = "";
        //獲取請求URI
        URI uri = getUri(url);
        if (uri != null) {
            HttpGet httpGet = new HttpGet(uri);
            requestConfig = requestConfigBuilder.setRedirectsEnabled(false).build();//設置自動重定向false
            httpGet.setConfig(requestConfig);
            if (!CollectionUtils.isEmpty(headers)) {
                Header[] header = new Header[headers.size()];
                httpGet.setHeaders(headers.toArray(header));
            }
 
            try {
                //執行get請求
                CloseableHttpResponse response = httpClient.execute(httpGet);
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {//301 302
                    Header header = response.getFirstHeader("Location");
                    if (header != null) {
                        location = header.getValue();
                    }
                }
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    EntityUtils.consume(entity);
                }
            } catch (Exception e) {
                logger.error("獲取http GET請求獲取 302 Location失敗 url======" + url, e);
                httpGet.abort();
            }
        }
        return location;
    }
 
    /**
     * 獲取輸入流
     */
    public InputStream getInputStream(String url) {
        //獲取請求URI
        URI uri = getUri(url);
        if (uri != null) {
            HttpGet httpGet = new HttpGet(uri);
            httpGet.setConfig(requestConfig);
            if (!CollectionUtils.isEmpty(headers)) {
                Header[] header = new Header[headers.size()];
                httpGet.setHeaders(headers.toArray(header));
            }
            //執行get請求
            try {
                CloseableHttpResponse response = httpClient.execute(httpGet);
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode != 200) {
                    logger.info("HttpClient status code :" + statusCode + "  request url===" + url);
                    httpGet.abort();
                } else {
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        InputStream in = entity.getContent();
                        return in;
                    }
                }
            } catch (Exception e) {
                logger.error("獲取http GET inputStream請求失敗 url======" + url, e);
                httpGet.abort();
            }
        }
        return null;
    }
 
    private URI getUri(String url) {
        URI uri = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(url);
            if (!CollectionUtils.isEmpty(nvps)) {
                uriBuilder.setParameters(nvps);
            }
            uri = uriBuilder.build();
        } catch (URISyntaxException e) {
            logger.error("url 地址異常", e);
        }
        return uri;
    }
 
 
    /**
     * from請求
     * @param url
     * @param params
     * @return
     */
    public static String form(String url, Map<String, String> params) {
        URL u = null;
        HttpURLConnection con = null;
        // 構建請求參數
        StringBuffer sb = new StringBuffer();
        if (params != null) {
            for (Entry<String, String> e : params.entrySet()) {
                sb.append(e.getKey());
                sb.append("=");
                sb.append(e.getValue());
                sb.append("&");
            }
            sb.substring(0, sb.length() - 1);
        }
        // 嘗試發送請求
        try {
            u = new URL(url);
            con = (HttpURLConnection) u.openConnection();
            //// POST 只能為大寫,嚴格限制,post會不識別
            con.setRequestMethod("POST");
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
            osw.write(sb.toString());
            osw.flush();
            osw.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                con.disconnect();
            }
        }
 
        // 讀取返回內容
        StringBuffer buffer = new StringBuffer();
        try {
            //一定要有返回值,否則無法把請求發送給server端。
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
            String temp;
            while ((temp = br.readLine()) != null) {
                buffer.append(temp);
                buffer.append("\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return buffer.toString();
    }
 
 
    public static void main(String[] args) {
        /*Map<String, String> map = new HashMap<>();
        JSONObject param = new JSONObject();
        param.put("activity_id","60:video:comets:10011#2");
        param.put("state",3);
        param.put("attr_tags","");
        param.put("msg","");
        map.put("param",param.toJSONString());
        String form = form("http://yp5ntd.natappfree.cc/CnInteraction/services/commentForHd/auditCallBackNew", map);
        System.out.println(form);*/
 
 
        JSONObject params = new JSONObject();
        params.put("video_id","60_8be004799c424688949704814ea0d16d");
        params.put("state",3+"");
        params.put("attr_tags","");
        params.put("msg","");
        HttpUtil httpUtil = HttpUtil.init();
        httpUtil.setParam("param",params.toJSONString());
        String URL ="http://mam.innerapi.cnlive.com/v1/inner/ShenHeInfo/shenheNotify?platform=AUDIT&token=2718c88930175686e40398994cead75c";
        Map<String, String> post = httpUtil.post(URL);
        System.out.println(post.get("result"));
    }
}
————————————————
版權聲明:本文為CSDN博主「TomHaveNoCat」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/tomhavenocat/article/details/90715904


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM