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

  • 相关阅读:
    微信客服系统开发SDK使用教程-给好友发消息任务
    微信客服系统开发SDK使用教程-客户端选择微信号登陆/登出通知
    微信客服系统开发SDK使用教程-客户端退出通知
    php优秀框架codeigniter学习系列——CI_Security类学习
    php优秀框架codeigniter学习系列——CI_Output类的学习
    php优秀框架codeigniter学习系列——CI_Router类学习
    My IELTS result has come out 我的雅思成绩出来了
    Travel notes in Vietnam
    asp.net学习
    makefile简单学习
  • 原文地址:https://www.cnblogs.com/itommy/p/10610373.html
Copyright © 2011-2022 走看看