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

ufk*_*ufk 5 java apache-httpclient-4.x

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

在发送帖子数据后,我需要检索html数据.

我发送的帖子数据是一个XML字符串,我收到的帖子数据是一个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;

}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我想出了这个函数,它发送一个请求并从响应中检索一个字符串.我认为它应该有效.我缺少的是我对postData没有任何作用.如何根据我的要求发送帖子数据?

ufk*_*ufk 7

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;

}
Run Code Online (Sandbox Code Playgroud)