我是靠谱客的博主 勤奋朋友,最近开发中收集的这篇文章主要介绍Java---发送HTTPS请求,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

主要jar包:https://pan.baidu.com/s/1o7EvbZk
httpclient-4.2.5
httpcore-4.2.4

X509TrustManager.java

package com.util.wxutil;

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

/**
 * 证书信任管理器(用于https请求)
 * Created by admin on 2017/11/14.
 */
public class MyX509TrustManager implements X509TrustManager {
    //检查客户端证书
    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }

    //检查服务器端证书
    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }

    //返回受信任的x509数组
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }
}

HttpsRequest.java
这边是要传递三个参数 URL 请求方式 参数

package com.util.wxutil;


import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.security.SecureRandom;

/**
 * Created by admin on 2017/11/14.
 */
public class HttpsREQUEST {
    /**
     * 发送https请求
     *
     * @param requestUrl
     *            请求地址
     * @param requestMethod
     *            请求方式(GET、POST)
     * @param outStr
     *            提交的数据
     * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
     */
    public static String HttpsRequest(String requestUrl, String requestMethod, String outStr) throws Exception{
//        创建 SSLContext
        SSLContext sslContext = SSLContext.getInstance("SSL","SunJSSE");
        TrustManager[] trustManagers = {new MyX509TrustManager()};
//        初始化
        sslContext.init(null,trustManagers,new SecureRandom());
//        获取 sslSocketFactory 对象
        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
//        设置当前实例使用

        StringBuffer buffer = null;
//      传递的URL
        URL url = new URL(requestUrl);
        HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
//      请求方式
        connection.setRequestMethod(requestMethod);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setSSLSocketFactory(sslSocketFactory);
        connection.connect();

        if (outStr != null){
            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(outStr.getBytes("utf-8"));
            outputStream.close();
        }

//        读取服务端内容
        InputStream inputStream = connection.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"utf-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        buffer = new StringBuffer();
        String line = null;
        while((line = bufferedReader.readLine()) != null){
            buffer.append(line);
        }
//        System.out.println(buffer.toString());
        return buffer.toString();
    }

}

下班了,这边潦草了点,不好意思;但是代码亲测,没有问题

最后

以上就是勤奋朋友为你收集整理的Java---发送HTTPS请求的全部内容,希望文章能够帮你解决Java---发送HTTPS请求所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部