使用Apache HttpClient通过Web代理进行Java通信失败

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

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

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

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

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

现在,一旦我们这样做,简单的下载工作正常。 例如,我有从我的Web服务器读取一些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没有被客户的Webfilter阻止,因为我们在Web浏览器中测试了地址。

这是我validation用户输入的注册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系统属性。 我知道属性已正确设置,但我仍然收到此身份validation错误。 查看从我的程序生成的日志显示了这个:

 checkRegistration INFO: Server response status code to checkRegistration: 407 

如何解决此身份validation错误?