天天看點

java post 證書_HttpPost忽略證書請求https

在請求 https 時,會遇到證書有一些問題的情況,是以在發送請求的時候需要忽略證書驗證。

(使用的主要jar包括httpclient-4.5.1.jar和httpcore-4.4.3.jar)

public String controlDevice() {

final String queryDevicesUrl="https://xxx.com:10000/dnaproxy/v1/device/control";

CloseableHttpClient client = HttpClients.createDefault();

client = (CloseableHttpClient)wrapClient(client);

HttpPost post = new HttpPost(queryDevicesUrl);

Long timestamp=System.currentTimeMillis();

Mapparams=new HashMap();

params.put("license", LICENSE);

params.put("did", "00000000000000000000b4430ddbcd0e");

params.put("pid", "000000000000000000000000384f0000");

params.put("tpid", "20280");

params.put("subdevdid", "");

params.put("subdevpid", "");

params.put("act", "set");

params.put("srv", "");

Listlist=new ArrayList();

list.add("pwr");

params.put("params", list);

List< List< Map< String,Integer>>> l1=new ArrayList< List< Map< String,Integer>>>();

List< Map< String,Integer>> l2=new ArrayList< Map< String,Integer>>();

Mapmap=new HashMap();

map.put("idx", 1);

map.put("val", 0);

l2.add(map);

l1.add(l2);

params.put("vals", l1);

String postData=new Gson().toJson(params);

// post.setHeader("Content-Type", "application/json");

post.addHeader("timestamp", timestamp.toString());

post.addHeader("signature", DigestUtils.shaHex(postData+timestamp+LICENSE));

post.addHeader("userid", USERID);

post.addHeader("accesskey", ACCESSKEY);

String result = "";

try {

StringEntity s = new StringEntity(postData);

// s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));

post.setEntity(s);

// 發送請求

CloseableHttpResponse httpResponse = client.execute(post);

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

System.out.println("請求伺服器成功");

result=EntityUtils.toString(httpResponse.getEntity(),"utf-8");

httpResponse.close();

System.out.println("結果:\n"+result);

} else {

System.out.println("請求服務端失敗");

httpResponse.close();

}

} catch (Exception e) {

System.out.println("請求異常");

e.printStackTrace();

}

return result;

}

public static HttpClient wrapClient(HttpClient base) {

try {

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

X509TrustManager tm = new X509TrustManager() {

public X509Certificate[] getAcceptedIssuers() {

return null;

}

public void checkClientTrusted(X509Certificate[] arg0,

String arg1) throws CertificateException {

}

public void checkServerTrusted(X509Certificate[] arg0,

String arg1) throws CertificateException {

}

};

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

SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(ctx,NoopHostnameVerifier.INSTANCE);

CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(ssf).build();

return httpclient;

} catch (Exception ex) {

ex.printStackTrace();

return HttpClients.createDefault();

}

}

說明:其中wrapClient方法就是建立一個不進行正式驗證的請求用戶端對象。

參考文檔:

1.http://www.th7.cn/Program/Java/201402/173791.shtml Https請求基本過程介紹;

2.http://blog.csdn.NET/kobejayandy/article/details/44284765 老版本Https請求的常見實作;

3.http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/CloseableHttpClient.html  Https請求使用類的最新官方API說明。