1. 程式人生 > >java實現httpclient傳送post請求

java實現httpclient傳送post請求

需求:現在要在java後端介面中直接請求客戶提供的其他介面來獲取所需要的資料,那麼就需要用到httpclient來做,下面做一個實現以記錄...


第一步:匯入所需要的jar包並寫一個工具類



1.post請求工具類


因為我們需要的協議是https協議,所以我做了一個httpsPostUtil



package com.qs.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.util.EntityUtils;

/**
 * 利用HttpClient進行post請求的工具類
 *
 * @author Devin <xxx>
 * @ClassName: HttpClientUtil
 * @Description: TODO
 * @date 2017年2月7日 下午1:43:38
 */
public class HttpsPostUtil {
    public static String doPost(String url, String charset) {
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try {
            httpClient = new SSLClient();
            httpPost = new HttpPost(url);
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
}

2.用於進行Https請求的HttpClient


package com.qs.util;

import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

/**
 * 用於進行Https請求的HttpClient
 *
 * @author Devin <xxx>
 * @ClassName: SSLClient
 * @Description: TODO
 * @date 2017年2月7日 下午1:42:07
 */
public class SSLClient extends DefaultHttpClient {
    public SSLClient() throws Exception {
        super();
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] chain,
                                           String authType) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] chain,
                                           String authType) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        ctx.init(null, new TrustManager[]{tm}, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = this.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
    }
}


第二步:java中呼叫方式

			String url = "https://www.aaa.com/sss/?a=" + a + "&b=" + b + "&c=" + c + "&d=" + d;
			String httpsRtn = HttpsPostUtil.doPost(url, "UTF-8");
			Map<String, Object> json = mapper.readValue(httpsRtn, Map.class);
			String id = (String) json.get("id");



如果有什麼問題,或者缺什麼工具,jar之類的  請聯絡我   [email protected]