posts - 710,  comments - 81,  views - 260万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
MyX509TrustManager.java
复制代码
package com.demo.until;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class MyX509TrustManager implements X509TrustManager {
    @Override
    public void checkClientTrusted(X509Certificate[] chain, String authType)
            throws CertificateException {
        // TODO Auto-generated method stub

    }

    @Override
    public void checkServerTrusted(X509Certificate[] chain, String authType)
            throws CertificateException {
        // TODO Auto-generated method stub

    }

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        // TODO Auto-generated method stub
        return null;
    }

    // 处理http请求 requestUrl为请求地址 requestMethod请求方式,值为"GET"或"POST"
    public static String httpRequest(String requestUrl, String requestMethod,
            String outputStr) {

        StringBuffer buffer = null;
        try {
            URL url = new URL(requestUrl);// 请求地址
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 创建连接对象
            conn.setDoOutput(true);// 是否输出
            conn.setDoInput(true);// 是否输入
            conn.setRequestMethod(requestMethod);// post Or get
            conn.setRequestProperty("Charset", "utf-8");
            conn.setFollowRedirects(false);
            conn.setInstanceFollowRedirects(false);
            conn.connect();// 发起连接
            // 往服务器端写内容 也就是发起http请求需要带的参数
            if (null != outputStr) {
                OutputStream os = conn.getOutputStream();
                os.write(outputStr.getBytes("utf-8"));
                os.close();
            }

            // 读取服务器端返回的内容
            // PrintWriter out = new PrintWriter(new
            // OutputStreamWriter(conn.getOutputStream(),"utf-8"));
            // out.println(obj);

            InputStream is = conn.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            buffer = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                buffer.append(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }

    /*
     * 处理https GET/POST请求 请求地址、请求方法、参数
     */
    public static String httpsRequest(String requestUrl, String requestMethod,
            String outputStr) {
        StringBuffer buffer = null;
        try {
            // 创建SSLContext
            SSLContext sslContext = SSLContext.getInstance("SSL");
            TrustManager[] tm = { new MyX509TrustManager() };
            // 初始化
            sslContext.init(null, tm, new java.security.SecureRandom());
            ;
            // 获取SSLSocketFactory对象
            SSLSocketFactory ssf = sslContext.getSocketFactory();
            URL url = new URL(requestUrl);
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod(requestMethod);
            // 设置当前实例使用的SSLSoctetFactory
            conn.setSSLSocketFactory(ssf);
            conn.connect();
            // 往服务器端写内容
            if (null != outputStr) {
                OutputStream os = conn.getOutputStream();
                os.write(outputStr.getBytes("utf-8"));
                os.close();
            }
            // 读取服务器端返回的内容
            InputStream is = conn.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "gbk");
            BufferedReader br = new BufferedReader(isr);
            buffer = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                buffer.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }

    public static void main(String[] args) {
        //测试
        String testUrl = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=xxxxxx";//淘宝获取手机号信息接口
        String url = "地址";
        String s = httpsRequest(url, "POST", null);
        System.out.println(s);
    }
}
复制代码

 这里测试的是小程序获取openid

复制代码
// 用code发起请求并获取oepnId
        MyX509TrustManager mx = new MyX509TrustManager();
        String s = mx
                .httpsRequest(
                        "https://api.weixin.qq.com/sns/jscode2session?appid=''&secret=''&js_code=''
                            &grant_type=authorization_code",
                        "POST", null);
        //转JSON
        Gson gson = new Gson();
        Map<String, Object> map = new HashMap<String, Object>();
        map = gson.fromJson(s, map.getClass());
        String openId = (String) map.get("openid"); 
复制代码

转载自:https://blog.csdn.net/qq_42129925/article/details/93891214

posted on   itprobie-菜鸟程序员  阅读(810)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2015-06-19 Newtonsoft 序列化和反序列化特殊处理
2015-06-19 jquery 页面滚动到底部自动加载插件集合
2015-06-19 Bootstrap 模态对话框只加载一次 remote 数据的解决办法
点击右上角即可分享
微信分享提示