zoukankan      html  css  js  c++  java
  • HttpClient 之 发送Https请求

    HttpClient包是一个优秀的Http请求的开源jar。

    本文Http工具类的封装基于HttpClient,封装后的工具类支持Https请求。

    但是由于项目的需要快速的实现,以下代码还可能会有点过时,但是要是可行的,并且相对比较简单。

    1.实现一个SSLClient的客户端

    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;
    
    public class SSLClient extends DefaultHttpClient {
    
        public SSLClient() throws Exception{
            super();
            SSLContext ctx = SSLContext.getInstance("TLS");
            X509TrustManager tm = new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] chain,
                                               String authType) throws CertificateException {
                }
                @Override
                public void checkServerTrusted(X509Certificate[] chain,
                                               String authType) throws CertificateException {
                }
                @Override
                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));
        }
    }

    2.HttpClientUtils工具类

    import com.google.common.base.Charsets;
    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.entity.StringEntity;
    import org.apache.http.util.EntityUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class HttpClientUtil {
    
        public static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
    
    
        public static String doPost(String url, String json) {
            HttpClient httpClient;
            HttpPost httpPost;
            String result = null;
            try {
                httpClient = new SSLClient();
                httpPost = new HttpPost(url);
                StringEntity entity = new StringEntity(json, Charsets.UTF_8);//解决中文乱码问题
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                httpPost.setEntity(entity);
                HttpResponse response = httpClient.execute(httpPost);
                if (response != null) {
                    HttpEntity resEntity = response.getEntity();
                    if (resEntity != null) {
                        result = EntityUtils.toString(resEntity, Charsets.UTF_8);
                    }
                }
            } catch (Exception e) {
                logger.error("调用访问https工具类, 抛错:", e);
            }
            return result;
        }
    }

    http://www.cnblogs.com/stevendes/p/5971706.html

  • 相关阅读:
    SQL的介绍及MySQL的安装
    git中级技能
    git基本用法
    git基本语法
    出租车数据分析
    使用Spark MLlib进行情感分析
    增量式编码器专题
    vue-loader的简单例子
    node爬虫(转)
    fs-extra 文件管理
  • 原文地址:https://www.cnblogs.com/itommy/p/10610373.html
Copyright © 2011-2022 走看看