要传递给Java应用程序以使用http代理进行身份validation的Java属性

我有一个Java应用程序试图通过http代理访问Web服务。 Java应用程序是我们无法访问源代码的第三方应用程序。

可以通过传递Java启动参数来配置它的启动。 我想知道一个人可以传递的java属性是什么,以便应用程序可以使用登录用户的NTLM凭据来validation代理连接?

当我通过https.proxyHost和https.proxyPort(即-Dhttps.proxyHost = abcd …到jvm命令行)时,我确实看到了日志的不同。 现在它失败了,下面有消息。

[WrapperSimpleAppMain] [AuthChallengeProcessor] ntlm authentication scheme selected INFO | jvm 5 | 2015/06/03 14:49:25 | 2015-06-03 14:49:25,380 INFO [WrapperSimpleAppMain] [HttpMethodDirector] No credentials available for NTLM @proxy.ins.dell.com:80 INFO | jvm 5 | 2015/06/03 14:49:25 | Exiting due to fatal exception. INFO | jvm 5 | 2015/06/03 14:49:25 | com.atlassian.bamboo.agent.bootstrap.RemoteAgentHttpException: HTTP status code 407 received in response to fingerprint request 

我试过传递http.proxyUser和http.proxyPassword。 那没用。 我想知道正确的配置是什么使Java应用程序透明地使用代理信息而无需进行代码更改。

谢谢

还需要为NTLM authnetication指定NT域才能工作。

 -Dhttp.proxyUser=MyDomain/username 

或通过设置

 -Dhttp.auth.ntlm.domain=MyDomain 

而且你还必须明确地指示HttpClient考虑系统属性,默认情况下不会这样做

  CloseableHttpClient client = HttpClients.createSystem(); 

要么

  CloseableHttpClient client = HttpClients.custom() .useSystemProperties() .build(); 

最后,我通过反复试验得出结论。 传递java.net.useSystemProxies = true以及https.proxyPort,https.proxyHost解决了这个问题。

基本上java vm命令行了

-Djava.net.useSystemProxies = true -Dhttps.proxyPort = 80 -Dhttps.proxyHost = proxyserver.mycompany.com

我没有通过https.proxyUser,https.proxyPassword。 我相信代理身份validation使用与我的登录NTLM凭据相同的凭据。

Apache HttpClient 4.5的一个工作示例。*

注意:除非使用HttpClients.custom().useSystemProperties().build();否则它不起作用HttpClients.custom().useSystemProperties().build();

 System.setProperty("http.proxyHost" , "myhost"); System.setProperty("http.proxyPort" , "myport"); System.setProperty("http.proxyUser" , "myuser"); System.setProperty("http.proxyPassword" , "mypassword"); CloseableHttpClient httpclient = HttpClients.custom().useSystemProperties().build(); try { HttpGet httpGet = new HttpGet("http://www.google.com"); CloseableHttpResponse httpResponse = httpclient.execute(httpGet); try { System.out.println(httpResponse.getStatusLine()); for (Header header : response.getAllHeaders()) { System.out.println("header " + header.getName() + " - " + header.getValue()); } String responseString = EntityUtils.toString(httpResponse.getEntity()); System.out.println("responseString :" + responseString); } finally { response.close(); } } catch (Exception exception) { exception.printStackTrace(); } finally { httpclient.close(); } 

您可以使用设置属性而不是使用System.setProperty

 -Dhttp.proxyHost="myhost" -Dhttp.proxyPort="myport" -Dhttp.proxyUser=myuser -Dhttp.proxyPassword="mypassword" 

重要提示 :如果您尝试访问HTTPS服务,则必须将属性更改为https 如果您使用https。*属性并访问http URL,则它也不起作用

 System.setProperty("https.proxyHost" , "myhost"); System.setProperty("https.proxyPort" , "myport"); System.setProperty("https.proxyUser" , "myuser"); System.setProperty("https.proxyPassword" , "mypassword"); CloseableHttpClient httpclient = HttpClients.custom().useSystemProperties().build(); try { HttpGet httpGet = new HttpGet("https://www.google.com"); 

API: https : //hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/

纯Java – 没有Apache HttpClient

如果使用java.net.HttpURLConnection类,则可以设置相同的https.proxyHost等属性

始终尊重使用https.proxyHost等为https:// …连接和http.proxyHost等为http:// …连接!

 String uri = "https://www.google.com/"; HttpURLConnection connection = (HttpURLConnection) new URL(uri).openConnection(); InputStream response = connection.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; int x = 0; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); x++; if (x > 4) { break;} } in.close(); response.close();