java发送https的get请求忽略证书

这是忽略证书的类:

import java.security.KeyManagementException;

import java.security.NoSuchAlgorithmException;

import java.security.cert.CertificateException;

import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;

import javax.net.ssl.TrustManager;

import javax.net.ssl.X509TrustManager;

import org.apache.http.client.HttpClient;

import org.apache.http.conn.ClientConnectionManager;

import org.apache.http.conn.scheme.Scheme;

import org.apache.http.conn.scheme.SchemeRegistry;

import org.apache.http.conn.ssl.SSLSocketFactory;

import org.apache.http.impl.client.DefaultHttpClient;

public class HttpsClient {

private static X509TrustManager tm =new X509TrustManager() {

public void checkClientTrusted(X509Certificate[] xcs, String string)throws CertificateException {

}

public void checkServerTrusted(X509Certificate[] xcs, String string)throws CertificateException {

}

public X509Certificate[] getAcceptedIssuers() {

return null;

}

};

@SuppressWarnings("deprecation")

public static HttpClient getInstance()throws KeyManagementException,NoSuchAlgorithmException {

HttpClient client =new DefaultHttpClient();

SSLContext ctx = SSLContext.getInstance("TLS");

ctx.init(null,new TrustManager[] {tm },null);

SSLSocketFactory ssf =new SSLSocketFactory(ctx);

ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

ClientConnectionManager ccm = client.getConnectionManager();

SchemeRegistry sr = ccm.getSchemeRegistry();

sr.register(new Scheme("https", ssf,443));

client =new DefaultHttpClient(ccm, client.getParams());

return client;

}

}

如何调用这个类:


java发送https的get请求忽略证书_第1张图片
image.png
String url ="https://。。。。。。。。。。?haId=" + hald;

JSONObject jsonResult =null;

//常规的http请求时这样的

//CloseableHttpClient httpClient = HttpClients.createDefault();​

//在这里获取HttpsClient的一个实例作为httpclient的一个对象

try {HttpClient httpsClient =null;

httpsClient = HttpsClient.getInstance();

Map headers =new HashMap();

headers.put("Accept","application/json;charset=utf-8");

headers.put("Content-Type","application/json;charset=utf-8");

headers.put("bshtoken", bshtoken);

//发送get请求

    HttpGet request =new HttpGet(url);

if (headers !=null && headers.size() >0) {

request.setHeaders(assemblyHeader(headers));}

HttpResponse response = httpsClient.execute(request);

if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

String strResult = EntityUtils.toString(response.getEntity());

jsonResult = JSONObject.parseObject(strResult);

url = URLDecoder.decode(url,"UTF-8");

}else {

System.out.println("通信异常:" + url);

}

}catch (IOException e) {

System.out.println("通信异常:" + url+e);

}catch (NoSuchAlgorithmException e) {

e.printStackTrace();

}catch (KeyManagementException e) {

e.printStackTrace();

}

System.out.println("++++++++++++++++++++++:" + jsonResult);

return null;

看一下打印结果:

image

其实我一直想探究一下http请求的情况这是很正常的处理方式之一就是忽略证书

其实还有一种就是自己的证书可以自己生成一个证书要手动添加,其实这个在我的小结证书问题中有这个问题怎么加入java的jdk中让他支持该证书。已经做了说明:

https://www.jianshu.com/p/2bdae3089e26

你可能感兴趣的:(java发送https的get请求忽略证书)