java 使用 apache httpClient 客户端将发布数据发送到 https 而无需 ssl 证书验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14561293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sending post data to https without ssl cert verification with apache httpClient client
提问by ufk
I need to send post data to an https url using the apache HttpClient package,
我需要使用 apache HttpClient 包将发布数据发送到 https url,
after sending the post data I need to retreive the html data.
发送帖子数据后,我需要检索 html 数据。
the post data that I'm sending is an XML string and the post data that I'm receving is an XML string.
我发送的帖子数据是一个 XML 字符串,我接收的帖子数据是一个 XML 字符串。
any information regarding the issue would be greatly appreciated.
任何有关该问题的信息将不胜感激。
I googled and i found examples on the internet that uses DefaultHttpClient that now in version 4 is deprecated. so I'd like to know how to properly use the new version of the client.
我用谷歌搜索,我在互联网上找到了使用 DefaultHttpClient 的示例,现在版本 4 已被弃用。所以我想知道如何正确使用新版本的客户端。
thanks.
谢谢。
update
更新
public String sendPost(final String request, final String postData) throws ClientProtocolException, IOException {
String result = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(request);
CloseableHttpResponse response = httpclient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity);
EntityUtils.consume(entity);
} finally {
response.close();
}
return result;
}
so far I came up with the this function that sends a request and retrieves a string from the response. I think it should work. the thing I'm missing is that I'm doing nothing with the postData. how do I sent post data with my request ?
到目前为止,我想出了发送请求并从响应中检索字符串的 this 函数。我认为它应该工作。我缺少的是我没有对 postData 做任何事情。我如何根据我的请求发送帖子数据?
回答by ufk
public String sendPost(final String request, final String postData) throws ClientProtocolException, IOException, NoSuchAlgorithmException, KeyManagementException {
String result = null;
SSLContext sslContext = SSLContext.getInstance("SSL");
// set up a TrustManager that trusts everything
sslContext.init(null, new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
System.out.println("getAcceptedIssuers =============");
return null;
}
public void checkClientTrusted(X509Certificate[] certs,
String authType) {
System.out.println("checkClientTrusted =============");
}
public void checkServerTrusted(X509Certificate[] certs,
String authType) {
System.out.println("checkServerTrusted =============");
}
} }, new SecureRandom());
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(new SSLSocketFactory(sslContext)).build();
HttpPost httpPost = new HttpPost(request);
ByteArrayEntity postDataEntity = new ByteArrayEntity(postData.getBytes());
httpPost.setEntity(postDataEntity);
CloseableHttpResponse response = httpclient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity);
EntityUtils.consume(entity);
} finally {
response.close();
}
return result;
}
回答by Katie
Here's another method using Apache 4.5:
这是使用 Apache 4.5 的另一种方法:
/////////////////
// Create SSL Client
/////////////////
CloseableHttpClient httpclient = null;
HttpHost target = new HttpHost('www.mysite.com', 443, "https");
SSLContext sslcontext = SSLContexts.createSystemDefault();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
sslcontext, new String[] { "TLSv1", "SSLv3" }, null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", sslConnectionSocketFactory)
.build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
httpclient = HttpClients.custom()
.setSSLSocketFactory(sslConnectionSocketFactory)
.setConnectionManager(cm)
.build();
/////////////////
// Send POST
/////////////////
HttpPost httppost = new HttpPost('/mypath');
ByteArrayEntity postDataEntity = new ByteArrayEntity(postData.getBytes());
httpPost.setEntity(postDataEntity);
CloseableHttpResponse response = httpclient.execute(target, httpPost);
/////////////////
// Get RESPONSE
/////////////////
try {
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity);
EntityUtils.consume(entity);
} finally {
response.close();
}