• okhttp 访问未认证自签证书 https ssl web.md


    Accepting self-signed certificates in OKHttp3 (codavel.com)

    验证可行

    package com.github.cclient.utils;
    
    import okhttp3.*;
    
    import javax.net.ssl.*;
    import java.io.IOException;
    import java.security.KeyManagementException;
    import java.security.NoSuchAlgorithmException;
    import java.security.cert.CertificateException;
    
    public class HttpsUtil {
        public static final MediaType JSON
                = MediaType.get("application/json; charset=utf-8");
    
        static private OkHttpClient getUnsafeOkHttpClient() {
            try {
                final TrustManager[] trustAllCerts = new TrustManager[]{
                        new X509TrustManager() {
    
                            @Override
                            public void checkClientTrusted(java.security.cert.X509Certificate[] chain,
                                                           String authType) {
                            }
    
                            @Override
                            public void checkServerTrusted(java.security.cert.X509Certificate[] chain,
                                                           String authType) {
                            }
                            @Override
                            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                                return new java.security.cert.X509Certificate[]{};
                            }
                        }
                };
    
    //            final SSLContext sslContext = SSLContext.getInstance(SSL);
                final SSLContext sslContext = SSLContext.getInstance("SSL");
                sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
    
                final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
    
                OkHttpClient.Builder builder = new OkHttpClient.Builder();
                builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
    
                builder.hostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                });
    
                return builder.build();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        public static String post(String url, String json) throws IOException {
            OkHttpClient client = getUnsafeOkHttpClient();
            RequestBody body = RequestBody.create(json,JSON);
            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            try (Response response = client.newCall(request).execute()) {
                return response.body().string();
            }
        }
        public static String get(String url) throws IOException {
            OkHttpClient client = getUnsafeOkHttpClient();
            Request request = new Request.Builder()
                    .url(url)
                    .get()
                    .build();
            try (Response response = client.newCall(request).execute()) {
                return response.body().string();
            }
        }
    
    }
    
    
  • 相关阅读:
    Json转换利器Gson之实例一-简单对象转化和带泛型的List转化 (转)
    JSON数据解析(转)
    JSON数据解析(GSON方式) (转)
    ASP.NET MVC学习之Ajax(完结)
    Newtonsoft.Json(Json.Net)学习笔记
    IDEA教程之导入maven项目
    idea language level 介绍
    .net core Asp.net Mvc Ef 网站搭建 vs2017 1)
    【MVC】快速构建一个图片浏览网站
    asp.netmvc 三层搭建一个完整的项目
  • 原文地址:https://www.cnblogs.com/zihunqingxin/p/14575125.html
Copyright © 2020-2023  润新知