http post请求接口 传输json

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/baidu_16217779/article/details/75531258
/**
 * http post请求
 */

public class HttpRequest {
private static PoolingClientConnectionManager conMgr = null;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
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.client.params.ClientPNames;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;



public class HttpRequest {
    private static PoolingClientConnectionManager conMgr = null;
    private static HttpParams param = null;

    static {
         param = new BasicHttpParams();
        Integer CONNECTION_TIMEOUT = 4 * 10000; //设置请求超时
        Integer SO_TIMEOUT = 4 * 10000; //设置等待数据超时时间
        Long CONN_MANAGER_TIMEOUT = 5000L; //连接不够用的时候等待超时时间
        param.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);
        param.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT);
        param.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, CONN_MANAGER_TIMEOUT);
        param.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true);
        conMgr = new PoolingClientConnectionManager();
        conMgr.setMaxTotal(200);
        conMgr.setDefaultMaxPerRoute(500);

    }

    public static String post(String url, Map<String, String> params) throws UnsupportedEncodingException {

        DefaultHttpClient httpClient = new DefaultHttpClient(conMgr);
        httpClient.setParams(param);
        httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
        HttpResponse httpResponse = null;
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        Set<String> keySet = params.keySet();
        for (String key : keySet) {
            nvps.add(new BasicNameValuePair(key, params.get(key)));
        }
        UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(nvps, "UTF-8");
        HttpPost post = new HttpPost(url);
        String response = null;
        try {
            post.setEntity(uefEntity);
            httpResponse = httpClient.execute(post);
            // response实体
            HttpEntity entity = httpResponse.getEntity();
            if (null != entity) {
                int statusCode = httpResponse.getStatusLine().getStatusCode();
                if (statusCode == HttpStatus.SC_OK) {
                    response = EntityUtils.toString(entity, "utf-8");
                    return response;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            post.releaseConnection();
        }
        return response;
    }
}



                                    
展开阅读全文

没有更多推荐了,返回首页