使用apache httpClient客户端将post数据发送到https而无需ssl证书验证 [英] sending post data to https without ssl cert verification with apache httpClient client

查看:70
本文介绍了使用apache httpClient客户端将post数据发送到https而无需ssl证书验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to send post data to an https url using the apache HttpClient package,

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

after sending the post data I need to retreive the html data.

我发送的帖子数据是一个 XML 字符串,我接收的帖子数据是一个 XML 字符串.

the post data that I'm sending is an XML string and the post data that I'm receving is an XML string.

如果您提供有关该问题的任何信息,我们将不胜感激.

any information regarding the issue would be greatly appreciated.

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

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.

谢谢.

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;

}

到目前为止,我想出了发送请求并从响应中检索字符串的 this 函数.我认为它应该工作.我缺少的是我没有对 postData 做任何事情.我如何通过我的请求发送帖子数据?

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 ?

推荐答案

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 httpClient客户端将post数据发送到https而无需ssl证书验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆