okhttp3 post json 请求 忽略证书

import okhttp3.*;
import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
/**
 * 发送http post 请求
 * 不需要忽略证书
 * @param url
 * @param json
 * @return
 */
public static String httpPost(String url, String json) {
    String res = "";
    OkHttpClient okHttpClient = new OkHttpClient().newBuilder().connectTimeout(5, TimeUnit.SECONDS)
            .readTimeout(5, TimeUnit.SECONDS).writeTimeout(5, TimeUnit.SECONDS).build();
    RequestBody requestBody = RequestBody.create(JSON, json);
    //创建一个请求对象
    Request request = new Request.Builder()
            .url(url)
            .post(requestBody)
            .build();
    //发送请求获取响应
    try {
        Response response = okHttpClient.newCall(request).execute();
        res = response.body().string();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return res;
}

/**
 * 发送http post 请求
 * https请求时判断是否需要忽略证书
 * @param url
 * @param json
 * @return
 */
public static String httpPostNoSSL(String url, String json) {
    String res = "";
    //设置忽略证书--------start-------
    X509TrustManager xtm = 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() {
            X509Certificate[] x509Certificates = new X509Certificate[0];
            return x509Certificates;
        }
    };

    SSLContext sslContext = null;
    try {
        sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, new TrustManager[]{xtm}, new SecureRandom());
    } catch (Exception e) {
        e.printStackTrace();
    }
    HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };
    //设置忽略证书--------end-------

    OkHttpClient okHttpClient = new OkHttpClient().newBuilder().hostnameVerifier(DO_NOT_VERIFY)
            .sslSocketFactory(sslContext.getSocketFactory(), xtm).connectTimeout(5, TimeUnit.SECONDS).readTimeout(5, TimeUnit.SECONDS)
            .writeTimeout(5, TimeUnit.SECONDS).build();


    RequestBody requestBody = RequestBody.create(JSON, json);
    //创建一个请求对象
    Request request = new Request.Builder()
            .url(url)
            .post(requestBody)
            .build();
    //发送请求获取响应
    try {
        Response response = okHttpClient.newCall(request).execute();
        res = response.body().string();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return res;
}

转载于:https://my.oschina.net/u/3239611/blog/995593

你可能感兴趣的:(okhttp3 post json 请求 忽略证书)