HttpsClientUtils

需要引用两个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());
	}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值