我有一个试图通过http代理访问Web服务的Java应用程序。 Java应用程序是我们无法访问源代码的第三方应用程序。
可以通过传递Java启动参数来配置它的启动。 我想知道一个人可以传递的java属性是什么,以便应用程序可以使用登录用户的NTLM凭据来验证代理连接?
当我通过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 <any realm>@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应用程序透明地使用代理信息而无需进行代码更改。
由于
答案 0 :(得分:2)
还需要为NTLM authnetication指定NT域才能工作。
-Dhttp.proxyUser=MyDomain/username
或通过设置
-Dhttp.auth.ntlm.domain=MyDomain
并且您还必须明确指示HttpClient考虑系统属性,默认情况下不会这样做
CloseableHttpClient client = HttpClients.createSystem();
或
CloseableHttpClient client = HttpClients.custom()
.useSystemProperties()
.build();
答案 1 :(得分:2)
最后,我通过反复试验得出结论。传递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。我相信代理身份验证使用与登录NTLM凭据相同的凭据。
答案 2 :(得分:2)
Apache HttpClient 4.5。*
的工作示例注意:除非您使用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网址
,它也不起作用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();