使用Apache HttpClient通过Web代理进行Java通信失败 [英] Java communication fails through web proxy using Apache HttpClient

查看:127
本文介绍了使用Apache HttpClient通过Web代理进行Java通信失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个桌面客户端,它将数据发送到Web服务器,我似乎无法通过代理服务器。

I have a desktop client that sends data to a web server and I can't seem to get through the proxy server.

更新:我在尝试通过代理进行通信时遇到407 HTTP错误。

Update: I am getting a 407 HTTP error when trying to communicate through the proxy.

从我的Web服务器下载信息时,一切都很好。一旦用户配置代理服务器(使用我写的对话框),下载工作正常。但是使用 org.apache.http.client.HttpClient 上传数据无效。

When downloading information from my web server, everything is fine. Once the user configures the proxy server (using a dialog box I wrote) the download works fine. But uploading data using org.apache.http.client.HttpClient is not working.

在从JDialog收集信息后,我正在使用这样的代码配置代理服务器。

I am configuring the proxy server with code like this after gathering the information from a JDialog.

        System.setProperty("http.proxyHost", proxyHost);
        System.setProperty("http.proxyPort", "" + portNumber);

现在,一旦我们这样做,简单下载就可以了。例如,我有从我的Web服务器读取一些xml数据的代码(见下文)。在客户的网络上,我的catch块中的错误在配置代理设置之前显示,然后一旦设置了正确的代理,一切正常。

Now, once we do that, simple downloads work fine. For instance, I have code that reads some xml data from my web server (see below). On the customer's network the error in my catch block was displayed before the proxy settings were configured and then everything worked fine once the correct proxy was set.

/**
 * Loads a collection of exams from the web site. The URL is determined by
 * configuration or registration since it is State specific.
 */
public static int importExamsWS(StringBuilder msg) {
    try {
        java.net.URL onlineExams = new URL(examURL);
        //Parse the XML data from InputStream and store it.
        return importExams(onlineExams.openStream(), msg);

    } 
    catch (java.net.UnknownHostException noDNS) {
        showError(noDNS, "Unable to connect to proctinator.com to download the exam file.\n"
                + "There is probably a problem with your Internet proxy settings.");
    }
    catch (MalformedURLException | IOException duh) {
        showFileError(duh);
    }
    return 0;
}

然而,当我尝试向网络服务器发送数据时,就好像正在忽略代理设置,并抛出IOException。即:

However, when I try to SEND data to the web server it is as if the proxy settings are being ignored and an IOException gets thrown. Namely:

org.apache.http.conn.HttpHostConnectException: Connection to http://proctinator.com:8080 refused

现在我知道端口8080没有被客户的网络过滤器阻止,因为我们在网络浏览器中测试了地址。

Now I know that port 8080 is not blocked by the customer's web filter because we tested the address in a web browser.

这是我验证用户输入的注册ID的代码:
更新:现在我也在这个方法中设置代理。

Here is my code for verifying the registration ID entered by the user: Update: Now I am also setting the proxy in this method.

//Registered is just an enum with ACTIVE, INACTIVE, NOTFOUND, ERROR
public static Registered checkRegistration(int id) throws IOException {    
    httpclient = new DefaultHttpClient();
    Config pref = Config.getConfig(); //stores user-entered proxy settings.
    HttpHost proxy = new HttpHost(pref.getProxyServer(), pref.getProxyPort());
    httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    URIBuilder builder = new URIBuilder();
    String path = "/Proctorest/rsc/register/" + id;
    try {
        builder.setScheme("http").setHost(server).setPort(8080).setPath(path);
        URI uri = builder.build();
        System.out.println("Connecting to " + uri.toString());
        HttpGet httpget = new HttpGet(uri);
        HttpResponse response = httpclient.execute(httpget);
        System.out.println(response.getStatusLine().toString());
        if(response.getStatusLine().getStatusCode()==200) {
            String msg = EntityUtils.toString(response.getEntity());
            GUI.globalLog.log(Level.INFO, "Server response to checkRegistration(" + id + "): " + msg);
            return Registered.stringToRegistered(msg);
        }
        else {
            GUI.globalLog.log(Level.INFO, "Server response status code to checkRegistration: " + 
                    response.getStatusLine().getStatusCode());
            return Registered.ERROR;
        }
    }
    catch(java.net.URISyntaxException bad) {
        System.out.println("URI construction error: " + bad.toString());
        return Registered.ERROR;
    }
}

我几乎肯定这个问题来自于代理服务器配置,但 SystemDefaultHttpClient的文档声称它需要使用系统属性 http.proxyHost http.proxyPort 。我知道属性已正确设置,但我仍然收到此身份验证错误。查看从我的程序生成的日志向我显示:

I'm almost positive that the problem is coming from the proxy server configuration, yet the docs for SystemDefaultHttpClient claim that it takes uses System properties for http.proxyHost and http.proxyPort . I know that the properties have been properly set, but I am still getting this authentication error. Viewing the logs generated from my program showed me this:

checkRegistration INFO: Server response status code to checkRegistration: 407 

如何解决此身份验证错误?

How do I resolve this authentication error?

推荐答案

我会开枪。如果代理服务器使用基本身份验证,您可以使用以下代码段作为示例:

I'll take a shot. If the proxy server is using basic authentication, you could use the following snippet as an example:

DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
    new AuthScope("PROXY HOST", 8080),
    new UsernamePasswordCredentials("username", "password"));

HttpHost targetHost = new HttpHost("TARGET HOST", 443, "https");
HttpHost proxy = new HttpHost("PROXY HOST", 8080);

httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); 

如果代理服务器正在使用NTLM身份验证,我不相信NTLM支持可用于所有代理服务器版本(它将与一些NTLM身份验证代理服务器版本一起使用 - 检查代理是否使用NTLM身份验证的v1或v2)。供参考&解决方法,您可以检查以下内容:

If the proxy server is using NTLM authentication, I do not believe NTLM support is available for all proxy server versions (it will work with some NTLM authentication proxy server versions - check if the proxy is using v1 or v2 of NTLM authentication). For reference & workarounds, you can check the following:

http://hc.apache.org/httpcomponents-client-ga/ntlm.html

http://htmlunit.sourceforge.net/ntlm.html

< a href =http://devsac.blogspot.com/2010/10/supoprt-for-ntlmv2-with-apache.html =noreferrer> http://devsac.blogspot.com/2010/10/ supoprt-for-ntlmv2-with-apache.html

根据您的代理服务器,您可能需要查看NTCredentials而不是UserPasswordCredentials。

Depending on your proxy server, you may need to look into NTCredentials instead of UserPasswordCredentials.

我还建议您考虑使用wireshark捕获网络数据包并检查代理服务器的响应,以确保导致问题的原因。

I would also suggest that you may wish to look into using wireshark to capture network packets and check the response from the proxy server to be absolutely sure what is causing the problem.

这篇关于使用Apache HttpClient通过Web代理进行Java通信失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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