Java发起post请求

package com.shucha.deveiface.biz.utils;

/**
 * @author tqf
 * @Description
 * @Version 1.0
 * @since 2020-09-23 15:48
 */
import com.alibaba.fastjson.JSON;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.params.HttpMethodParams;


public class HttpClientUtil
{
    //private static Logger log = Logger.getLogger(HttpClientUtil.class.getClass());

    public static final String URL_PARAM_DECODECHARSET_UTF8 = "UTF-8";

    private static final String URL_PARAM_CONNECT_FLAG = "&";

    private static final String EMPTY = "";

    private static MultiThreadedHttpConnectionManager connectionManager = null;

    private static int connectionTimeOut = 25000;

    private static int socketTimeOut = 25000;

    private static int maxConnectionPerHost = 20;

    private static int maxTotalConnections = 20;

    static
    {
        connectionManager = new MultiThreadedHttpConnectionManager();
        connectionManager.getParams().setConnectionTimeout(connectionTimeOut);
        connectionManager.getParams().setSoTimeout(socketTimeOut);
        connectionManager.getParams().setDefaultMaxConnectionsPerHost(maxConnectionPerHost);

        connectionManager.getParams().setMaxTotalConnections(maxTotalConnections); }

    private static HttpClient client = new HttpClient(connectionManager);

    public static String URLPost(String url, Map<String, String> headParam, Map<String, Object> params)
    {
        String response = "";
        PostMethod method = null;
        try {
            method = new PostMethod(url);
            String transJson = JSON.toJSONString(params);
            Iterator headInfo = headParam.keySet().iterator();
            while (headInfo.hasNext()) {
                String key = (String)headInfo.next();
                String value = (String)headParam.get(key);
                method.setRequestHeader(key, value);
            }

            RequestEntity se = new StringRequestEntity(transJson, "application/json", "UTF-8");
            method.setRequestEntity(se);

            method.getParams().setParameter("http.method.retry-handler", new DefaultHttpMethodRetryHandler());

            method.getParams().setParameter("http.socket.timeout", Integer.valueOf(10000));

            int statusCode = client.executeMethod(method);
            if (statusCode == 200) {
                response = method.getResponseBodyAsString();
            } else {
                //log.info("响应状态码 = " + method.getStatusCode());
                response = method.getResponseBodyAsString();
            }
        } catch (Exception e) {
            //log.info("发生异常" + e.getMessage());
        }
        return response;
    }

    public static String URLPostForm(String url, Map<String, String> params,String Authorization)
    {
        String response = "";
        PostMethod postMethod = null;
        try {
            postMethod = new PostMethod(url);
            postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            Set<String> keySet = params.keySet();
            for (String key : keySet) {
                String value = (String)params.get(key);
                postMethod.addParameter(key, value);
            }
            int statusCode = new HttpClient().executeMethod(postMethod);
            if (statusCode == 200) {
                response = postMethod.getResponseBodyAsString();
                //log.info(response);
            } else {
                //log.info("响应状态码 = " + postMethod.getStatusCode());
            }
        } catch (HttpException e) {
            //log.info("发生致命的异常,可能是协议不对或者返回的内容有问题" + e);
        } catch (IOException e) {
            //log.info("发生网络异常" + e);
        } finally {
            if (postMethod != null) {
                postMethod.releaseConnection();
                postMethod = null;
            }
        }
        return response;
    }
}

 

posted @ 2022-08-04 19:12  码奴生来只知道前进~  阅读(4)  评论(0编辑  收藏  举报