需要引用两个jar包:
okhttp-3.6.0.jar
okio-1.11.0.jar
package com.utils;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import okhttp3.CipherSuite;
import okhttp3.ConnectionSpec;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* @author jhm
* @version 1.0.0
* @ClassName HttpsClientDemo.java
* @Description TODO
* @createTime 20200617 16:24:11
*/
public class HttpsClientDemo {
private static final MediaType MEDIA_JSON = MediaType.parse("application/json;charset=utf-8");
private static X509TrustManager TRUST_ALL = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
};
private static HostnameVerifier TRUST_ALL_HOSTNAME_VERIFIER = new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
};
private static OkHttpClient getOkHttpClient() throws NoSuchAlgorithmException, KeyManagementException {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] { TRUST_ALL }, null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
clientBuilder.sslSocketFactory(sslSocketFactory, TRUST_ALL);
ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).cipherSuites(
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA256, CipherSuite.TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, CipherSuite.TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
).build();
clientBuilder.connectionSpecs(Collections.singletonList(spec));
clientBuilder.hostnameVerifier(TRUST_ALL_HOSTNAME_VERIFIER);
OkHttpClient client = clientBuilder.build();
return client;
}
public static JSONObject httpsPost(String url, String body, String header) {
try {
OkHttpClient client = getOkHttpClient();
RequestBody formBody;
formBody = RequestBody.create(MEDIA_JSON, body);
// 根据自己情况修改请求头等信息
Request request = new Request.Builder().post(formBody).url(url).addHeader("token", header).build();
Response response = client.newCall(request).execute();
String string = response.body().string();
JSONObject jsonObject = JSONObject.parseObject(string);
return jsonObject;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
Map<String, Object> body = new HashMap<String, Object>();
body.put("username", "admin");
body.put("password", "111111");
JSONObject jsonObject = httpsPost("https://11.12.95.205:8443/smcs/api/public/login", JSON.toJSONString(body),
"");
System.out.println("网络设备上报结果为{}" + jsonObject.toJSONString());
}
}