系统运维系列 之JAVA利用HttpClient进行HTTPS接口调用

news/2024/6/19 0:31:07 标签: https, http, apache, java, webservice
http://www.w3.org/2000/svg" style="display: none;">

java访问https>https出现如下类似错误:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed…

解决方法是跳过验证证书的步骤,优点是实现起来方便,缺点是安全性有可能存在问题。
代码为:

java">//1.为了避免需要证书,所以用一个类继承DefaultHttpClient类,忽略校验过程。
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
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;

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>https", 443, ssf));
    }
}

//2.创建一个利用HttpClient发送post请求的工具类。
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

public class HttpClientUtil {
    @SuppressWarnings("resource")
    public static String doPost(String url,String jsonstr,String charset){
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try{
            httpClient = new SSLClient();
            httpPost = new HttpPost(url);
            httpPost.addHeader("Content-Type", "application/json");
            StringEntity se = new StringEntity(jsonstr);
            se.setContentType("text/json");
            se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
            httpPost.setEntity(se);
            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;
    }
}

//3.测试代码。
直接调用HttpClientUtil.doPost方法即可。

参考资料:
https>https://www.cnblogs.com/luchangyou/p/6375166.html JAVA利用HttpClient进行HTTPS接口调用


http://www.niftyadmin.cn/n/1223852.html

相关文章

办公软件系列 之excel应用2

本篇小博客为excel应用2: 问题描述:想在单元格内截取需要的字符串: 例子1:字符串为:CSDN CSDN123 CSDN123456 截取第一个CSDN,公式为:MID(A1,1,4) 具体公式含义为: MID(text,start_…

安卓开发系列 之安卓中TAB页的写法

1 介绍 安卓中TAB页的功能与WEB开发类似,就是利用空间复用展示更多的信息,使用Tab标签页控件,可以在同一个空间里放置更多内容。 TabActivity继承Activity,主要功能是实现多个activity或者view之间的切换和显示,要使用…

数据分析系列 之pandas用例分析1

1 pandas介绍: pandas是基于NumPy的一种工具,它是python的一个数据分析包,最初由AQR Capital Management于2008年4月开发,并于2009年底开源出来,目前由专注于Python数据包开发的PyData开发team继续开发和维护&#xff…

数据分析系列 之FP-growth算法介绍

1 基本概念: FP-growth,即 Frequent Pattern Growth,它通过构建 FP 树(即 Frequent Pattern Tree)这样的数据结构,巧妙得将数据存储在 FP 树中,只需要在构建 FP 树时扫描数据库两次,后续处理就不需要再访问…

办公软件系列 之excel应用3

这个小专题讲解一下Excel中averageif函数的使用方法: 1 背景问题: 在excel中可能会遇到这样的问题,如下面的数据: IDsSubIDsNumsAA110AA215BB112CC110CC220CC330 需要按照第一列中的大类ID求平均数,对于A中平均数为1…

啊,ASP.NET2.0在干什么?

我在研究缓存与数据库的同步实现的机制时,遇然间发现绝对让人喷鼻血,毫无疑问的零距离X级大接触。先看几幅图片: 这是以前的aspnet_regsql.exe工具,用于针对Sql Server2000及Sql Server7实现SqlCacheDependency功能的&#x…

数据分析系列 之python中遍历列表的几种方法

1 引入: 接触过C、java和python,目前经常使用的是java,C和python也忘的差不多啦哈哈…但是呢python现在经常使用,写脚本做数据处理等如果现查资料,感觉也挺不方便的。干脆就几个常用的用途复习一下吧… 2 正式开始&am…

数据分析系列 之python中字符串内容介绍

1 python中单引号、双引号、三引号的用法总结 可以通过单引号或双引号创建字符串。例如:a‘abc’; b“123” 使用两种引号的好处是可以创建本身就包含引号的字符串,而不需要使用转义字符。 主要使用场景是:*本质上单引号和双引号都可以表示一…