java实现httpclient发送post请求

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_24138151/article/details/78688390

需求:现在要在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]

猜你喜欢

转载自blog.csdn.net/qq_24138151/article/details/78688390