5

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

更新:尝试通过代理进行通信时出现 407 HTTP 错误。

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

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

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

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

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

但是,当我尝试将数据发送到 Web 服务器时,就好像代理设置被忽略并抛出了 IOException。即:

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

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

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

//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.proxyHosthttp.proxyPort. 我知道属性已正确设置,但我仍然收到此身份验证错误。查看从我的程序生成的日志向我展示了这一点:

checkRegistration INFO: Server response status code to checkRegistration: 407 

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

4

3 回答 3

5

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

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 身份验证代理服务器版本 - 检查代理使用的是 v1 还是 v2 的 NTLM 身份验证)。有关参考和解决方法,您可以检查以下内容:

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

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

http://devsac.blogspot.com/2010/10/supoprt-for-ntlmv2-with-apache.html

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

我还建议您可能希望考虑使用wireshark 来捕获网络数据包并检查来自代理服务器的响应以绝对确定导致问题的原因。

于 2012-07-08T23:08:02.277 回答
1

只是为了完整起见,因为这个问题是在搜索 JVM http.proxy 属性时发现的,如果使用 JVM 基础架构代理,可以使用以下方式提供用户名和密码:

System.setProperty("http.proxyUser",proxyUserName)
System.setProperty("http.proxyPassword",proxyUsePassword).
于 2012-07-11T17:08:59.213 回答
0

出于某种原因,在客户端上设置参数对我不起作用。

但是使用http方法。

于 2014-03-07T15:41:17.607 回答