有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

ApacheHttpClient 4。x要传递给Java应用程序以使用http代理进行身份验证的Java属性

我有一个Java应用程序,它试图通过http代理访问web服务。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。代理密码。那没用。 我想知道正确的配置是什么,使Java应用程序透明地使用代理信息,而不必更改代码

谢谢


共 (3) 个答案

  1. # 1 楼答案

    通过反复试验,我终于找到了答案。传递java。网useSystemProxies=true以及https。代理端口,https。proxyHost解决了这个问题

    基本上,javavm命令行

    -Djava。网useSystemProxies=true-Dhttps。proxyPort=80-Dhttps。proxyHost=proxyserver。我的公司。com

    我不需要通过https。代理用户,https。代理密码。我相信代理身份验证使用的凭据与我的登录NTLM凭据相同

  2. # 2 楼答案

    还需要为NTLM身份验证指定NT域才能工作

    -Dhttp.proxyUser=MyDomain/username
    

    或者通过设置

    -Dhttp.auth.ntlm.domain=MyDomain
    

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

     CloseableHttpClient client = HttpClients.createSystem();
    

     CloseableHttpClient client = HttpClients.custom()
         .useSystemProperties()
         .build();
    
  3. # 3 楼答案

    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();
    }
    

    而不是使用系统。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

    您可以设置相同的https。如果使用java,则使用proxyHost etc属性。网HttpURLConnection

    始终尊重使用https。代理主机等https://...连接和http。代理主机等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();