1. 程式人生 > >http post請求介面 傳輸json

http post請求介面 傳輸json

/**
 * 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;
    }
}