@Test
public void test4() throws Exception {
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts
.custom()
.loadTrustMaterial(new File("D:\\tomcat.keystore"),
"1qaz2wsx".toCharArray(), new TrustSelfSignedStrategy())
.build();
// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext, new String[] { "TLSv1" }, null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
HttpPost httpPost = null;
CloseableHttpResponse response = null;
try {
httpPost = new HttpPost(
"https://58.215.50.52:38443/openapi/openapi");
String params = getParam();
HttpEntity entity_request = new StringEntity(params, "UTF-8");
httpPost.setEntity(entity_request);
System.out.println("Executing request " + httpPost.getRequestLine());
response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
print(entity.getContent());
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(entity);
} catch (Exception e) {
e.printStackTrace();
} finally {
response.close();
httpclient.close();
}
}
private void print(InputStream is) throws IOException{
int len = 0;
byte b[] = new byte[1024];
while((len = is.read(b)) != -1){
System.out.println(new String(b,Charset.forName("UTF-8")));
}
}
private String getParam(){
JsonObject jObj = new JsonObject();
jObj.addProperty("cmd", "register");
JsonObject jsonElement1 = new JsonObject();
jsonElement1.addProperty("devKey", "rundong123");
jObj.add("auth", jsonElement1);
JsonObject jsonElement2 = new JsonObject();
jsonElement2.addProperty("userName", "rund");
jsonElement2.addProperty("password", "000000");
jsonElement2.addProperty("email", "rd@163.com");
jsonElement2.addProperty("mobile", "13012345678");
jObj.add("params", jsonElement2);
return jObj.toString();
}
httpcomponents https请求实例
最新推荐文章于 2024-08-07 09:39:53 发布