Java HttpClient - 通过代理发布文件 [英] Java HttpClient - Post with file through Proxy

查看:28
本文介绍了Java HttpClient - 通过代理发布文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用代理从我的网络应用程序调用外部网站.此外,需要在此外部网站上执行 POST 请求.

I try calling an external website from my webapplication with a proxy. Furthermore and need to execute a POST Request at this external website.

我正在使用:tomcat7、org.apache.httpcomponents 4.3.4、spring.

I'm using: tomcat7, org.apache.httpcomponents 4.3.4, spring.

以下,没有代理,工作,我得到一个响应状态200";

Following, without proxy, works and I get a response status '200';

    // uri = "https://punkte.eiv-fobi.de/upload/upload.do"
    private HttpStatus sendPost(URI uri, File file)
        throws ClientProtocolException, IOException {
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost httpPost = new HttpPost(uri);

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    FileBody fileBody = new FileBody(file, ContentType.MULTIPART_FORM_DATA);
    builder.addPart(PART_NAME, fileBody);
    httpPost.setEntity(builder.build());

    HttpResponse response = httpClient.execute(httpPost);

    return HttpStatus.valueOf(response.getStatusLine().getStatusCode());
    }

现在我尝试添加代理:

    // uri = "https://punkte.eiv-fobi.de/upload/upload.do"
public HttpStatus sendPostWithProxy(URI uri, File file) throws Exception {
    try {
        // JVM Parameter: -Dhttps.proxyHost and -Dhttps.proxyPort
        String proxyHost = System.getProperty("https.proxyHost");
        String proxyPort = System.getProperty("https.proxyPort");

        HttpClient httpClient = HttpClientBuilder.create().build();
        // CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(uri);
        HttpHost proxy = new HttpHost(proxyHost,
                Integer.valueOf(proxyPort), "https");

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        FileBody fileBody = new FileBody(file,
                ContentType.MULTIPART_FORM_DATA);
        builder.addPart(PART_NAME, fileBody);
        httpPost.setEntity(builder.build());

        RequestConfig config = RequestConfig.custom().setProxy(proxy)
                .build();
        httpPost.setConfig(config);

        HttpResponse response = httpClient.execute(httpPost);

        return HttpStatus.valueOf(response.getStatusLine().getStatusCode());
    } catch (Exception e) {
        LOGGER.error(
                "exception occurred.",
                e);
    }
    return null;
}

出现以下异常:

javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

我怎么了?替代方案?

推荐答案

我能想到几个可能导致这种情况的问题:

I can think of a few issues that could cause this:

由于您使用的是 https 代理并请求 https url(SSL/TLS 端点安全),您可能会遇到证书验证问题.检查 https 代理的这个很好的解释:使用 Http 代理与使用 https 代理的优缺点?

Since you are using an https proxy and requesting an https url (SSL/TLS endpoint security) you may have problems with certificate validation. Check this good explanation for https proxies: Pros and cons of using a Http proxy v/s https proxy?

这篇关于Java HttpClient - 通过代理发布文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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