1

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

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

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());
    }

now I tried this adding the proxy:

    // 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;
}

getting following exception:

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

What Do Im wrong? Alternatives?

0

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.