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

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

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