我尝试使用代理从我的webapplication调用外部网站。此外,需要在此外部网站上执行POST请求。
我正在使用:tomcat7,org.apache.httpcomponents 4.3.4,spring。
以下,没有代理,工作,我得到回复状态' 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?
我错了什么?替代?
答案 0 :(得分:1)
我可以想到一些可能导致这种情况的问题:
您确定您的https代理不需要身份验证吗?在这种情况下,您可能需要在上下文中设置凭据并使用上下文和客户端执行请求。请查看此示例:http://www.the-swamp.info/blog/java-request-through-proxy/
由于您使用的是https代理并请求https网址(SSL / TLS端点安全性),因此您可能会遇到证书验证问题。请查看https代理的详细说明:Pros and cons of using a Http proxy v/s https proxy?