来自SOCKS服务器的错误回复,而我使用HTTP代理(使用Apache HTTP库)

ToF*_*oFi 8 java proxy http-proxy apache-httpclient-4.x

我知道有很多关于错误的问题,这些错误Malformed reply from SOCKS server主要指向代理的错误配置.

但是,就我而言,我正在使用系统HTTP(!)代理来执行带有apache httpclient库4.3.5(httpcore 4.3.2)的POST请求,如下所示:

SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(
        ProxySelector.getDefault());
CloseableHttpClient httpclient = HttpClients.custom()
        .setRoutePlanner(routePlanner)
        .build();
Run Code Online (Sandbox Code Playgroud)

这也是/sf/answers/1452048181/中的建议

在大多数情况下,这个httpclient和以下的http POST请求都能很好地工作.但是,在一个客户,它失败并出现以下日志错误:

Mrz 05, 2015 10:11:04 AM org.apache.http.impl.execchain.RetryExec execute
Information: I/O exception (java.net.SocketException) caught when processing request to {}->http://proxy.local:80->http://my-webservice.tld:80: Malformed reply from SOCKS server
Mrz 05, 2015 10:11:04 AM org.apache.http.impl.execchain.RetryExec execute
Information: Retrying request to {}->http://proxy.local:80->http://my-webservice.tld:80
Run Code Online (Sandbox Code Playgroud)

本地系统代理设置(在Windows 7上)未配置为SOCKS代理,而是配置为HTTP代理!我证实了这一点通过登录不同RoutePlannerProxy参数:

TunnelType: PLAIN
TargetHost: http://my-webservice.tld:80
ProxyHost: http://proxy.local:80
ProxyPort: 80
ProxyType: HTTP
Run Code Online (Sandbox Code Playgroud)

但是,我的POST请求未正确发送.在代理日志文件中,它显示如下:

2015-03-05 10:11:04 119973 192.168.124.111 0 TCP_ERR_MISS 0 4 unknown - - / - - - - - - PROXIED - - xxx.xxx.xxx.xxx SG-HTTP-Service
Run Code Online (Sandbox Code Playgroud)

这是我的uploadFile()方法,它使用上面提到的SystemDefaultRoutePlanner并创建并执行HttpPost:

private String uploadFile(File fileToUpload) throws Exception {

    SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(ProxySelector.getDefault());
    CloseableHttpClient httpclient = HttpClients.custom()
            .setRoutePlanner(routePlanner)
            .build();

    try {
        HttpPost httppost = new HttpPost(webserviceURL);

        MultipartEntityBuilder requestEntity = MultipartEntityBuilder.create();

        requestEntity.addPart("project", new StringBody(paramProjekt, ContentType.TEXT_PLAIN));
        requestEntity.addPart("param1", new StringBody(param1, ContentType.TEXT_PLAIN));
        requestEntity.addPart("param2", new StringBody(param2, ContentType.TEXT_PLAIN));
        requestEntity.addPart("debug", new StringBody(paramDebug, ContentType.TEXT_PLAIN));
        requestEntity.addPart("xmlFile", new FileBody(fileToUpload));

        ProgressHttpEntityWrapper.ProgressCallback progressCallback = new ProgressHttpEntityWrapper.ProgressCallback() {

            @Override
            public void progress(float progress) {
                int min = 5;
                int max = 40;
                int diff = max - min;
                gui.updateProgress(min + (int)(diff * progress / 100));

                if (progress == 100) {
                    gui.setStep(GUI.STEP.PROCESSING);
                }
            }
        };

        httppost.setEntity(new ProgressHttpEntityWrapper(requestEntity.build(), progressCallback));

        logger.info("executing request " + httppost.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httppost);

        try {
            logger.info("Response Status: " + response.getStatusLine());
            HttpEntity resEntity = response.getEntity();

            if (resEntity != null) {
                String responseString = EntityUtils.toString(resEntity);
                return responseString;
            }

            EntityUtils.consume(resEntity);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            response.close();
        }

    } finally {
        httpclient.close();
    }

    return null;
}
Run Code Online (Sandbox Code Playgroud)

我的日志文件中的最后一件事是executing request日志信息,但Response Status:日志信息从未显示...

Mrz 05, 2015 10:09:04 AM de.company.product.WebserviceClient.WebserviceClient uploadFile
Information: executing request POST http://my-webservice.tld HTTP/1.1
Mrz 05, 2015 10:11:04 AM org.apache.http.impl.execchain.RetryExec execute
Information: I/O exception (java.net.SocketException) caught when processing request to {}->http://proxy.local:80->http://my-webservice.tld:80: Malformed reply from SOCKS server
Mrz 05, 2015 10:11:04 AM org.apache.http.impl.execchain.RetryExec execute
Information: Retrying request to {}->http://proxy.local:80->http://my-webservice.tld:80
Run Code Online (Sandbox Code Playgroud)

有没有人知道为什么

  • Malformed reply from SOCKS server当系统中定义了HTTP代理并且我使用系统代理设置时,它显示错误?
  • 如何配置httpclienthttppost以正确的方式识别系统HTTP代理设置,并使用和不使用HTTP代理?