使用apache httpClient客户端将发布数据发送到https而不使用ssl证书validation

我需要使用apache HttpClient包将发布数据发送到httpsurl,

在发送post数据后,我需要检索html数据。

我发送的post数据是一个XML字符串,我收到的post数据是一个XML字符串。

任何有关该问题的信息将不胜感激。

我用谷歌搜索,我在互联网上找到了使用DefaultHttpClient的示例,现在版本4已弃用。 所以我想知道如何正确使用新版本的客户端。

谢谢。

更新

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; } 

到目前为止,我想出了这个函数,它发送一个请求并从响应中检索一个字符串。 我认为它应该工作。 我缺少的是我对postData一无所知。 如何根据我的要求发送post数据?

 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; } 

这是使用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 socketFactoryRegistry = RegistryBuilder.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(); }