//POST
SkipHttpsUtil skipHttpsUtil=newSkipHttpsUtil();
CloseableHttpClient httpclient= null;
CloseableHttpResponse response= null;try{
httpclient=(CloseableHttpClient)skipHttpsUtil.wrapClient();
HttpPost post= newHttpPost(url);
String json= "{\"image\":\""+"ddd"+
"\",\"Type\":\""+"ddddd"+
"\",\"Flag\":\""+"dddd"+"\"}";
StringEntity postingString= new StringEntity(json,"utf-8");//json传递
post.setEntity(postingString);
post.setHeader("Content-type", "application/json");
response=httpclient.execute(post);
String result=EntityUtils.toString(response.getEntity());
JSONObject js=JSONObject.parseObject(result);
}catch(Exception e) {
e.printStackTrace();
mResultCode= "E";return false;
}finally{try{
response.close();
httpclient.close();
}catch(IOException e) {
e.printStackTrace();
mResultCode= "E";return false;
}
}
//GET
public static String doGet(String strUrl ){
String strReturn="";
HttpGet httpGet = new HttpGet(strUrl);
CloseableHttpClient httpclient = null;
CloseableHttpResponse response1=null;
try {
httpclient = (CloseableHttpClient)wrapClient();//HttpClients.createDefault();
response1 = httpclient.execute(httpGet);
HttpEntity entity1 = response1.getEntity();
strReturn=EntityUtils.toString(entity1) ;
EntityUtils.consume(entity1);
}catch(Exception e){
e.printStackTrace();
}finally {
try {
if(response1!=null)
response1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(strReturn);
return strReturn;
}packagecom.life.util;importjava.security.cert.CertificateException;importjava.security.cert.X509Certificate;importjavax.net.ssl.SSLContext;importjavax.net.ssl.TrustManager;importjavax.net.ssl.X509TrustManager;importorg.apache.http.client.HttpClient;importorg.apache.http.conn.ssl.NoopHostnameVerifier;importorg.apache.http.conn.ssl.SSLConnectionSocketFactory;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClients;importorg.apache.log4j.Logger;/**
*skipHttpsUtil类* Description: httpclient跳过https验证
*@author: hsw
* @CreateDate: 2020-08-26*/
public classSkipHttpsUtil {private static Logger logger = Logger.getLogger(SkipHttpsUtil.class);//绕过证书
public staticHttpClient wrapClient() {try{
SSLContext ctx= SSLContext.getInstance("TLS");
X509TrustManager tm= newX509TrustManager() {publicX509Certificate[] getAcceptedIssuers() {return null;
}public voidcheckClientTrusted(X509Certificate[] arg0,
String arg1)throwsCertificateException {
}public voidcheckServerTrusted(X509Certificate[] arg0,
String arg1)throwsCertificateException {
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLConnectionSocketFactory ssf= newSSLConnectionSocketFactory(
ctx, NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpclient=HttpClients.custom()
.setSSLSocketFactory(ssf).build();returnhttpclient;
}catch(Exception e) {returnHttpClients.createDefault();
}
}public static voidmain(String[] args) {
}
}