【问题标题】:Proxy settings in a java programjava程序中的代理设置
【发布时间】:2013-01-10 03:55:58
【问题描述】:

我正在尝试通过 Eclipse 中的 java 程序使用从 wsdl 生成的客户端连接到 Web 服务。我通过代理服务器传递我的请求。但似乎该请求没有通过。相同的代理设置在 SoapUI 上运行良好。请在下面找到我设置的系统属性。

Properties props= new Properties(System.getProperties()); 

props.put("http.proxySet", "true"); 

props.put("http.proxyHost", "10.x.x.x"); 

props.put("http.proxyPort", "80");

props.put("http.proxyUser","domainName\\xxx");

props.put("http.proxyPassword","xxx");

Properties newprops = new Properties(props);

Java 程序抛出异常为java.net.UnknownHostException:

我错过了什么?

【问题讨论】:

  • 在那之后你用你的“newProps”做什么?
  • 哪个主机未知?代理服务器还是 WebService 服务器?
  • 我将其设置为系统属性。顺便说一句,我也在尝试一个不同的代理,但我得到了一个不同的异常,javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
  • 网络服务服务器未知。
  • 您的 Web 服务是否通过 https 运行?如果是,则尝试设置 https 代理配置。 props.put("https.proxyHost", "10.x.x.x"); props.put("https.proxyPort", "80");

标签: java proxy


【解决方案1】:
java -Dhttp.proxyHost=proxyhostURL
     -Dhttp.proxyPort=proxyPortNumber
     -Dhttp.proxyUser=someUserName
     -Dhttp.proxyPassword=somePassword javaClassToRun

http://i4t.org/2007/05/04/java-http-proxy-settings/

【讨论】:

    【解决方案2】:

    我使用以下代码(并且它有效):

        String host = "10.x.x.x";
        String port = "80";
        System.out.println("Using proxy: " + host + ":" + port);
        System.setProperty("http.proxyHost", host);
        System.setProperty("http.proxyPort", port);
        System.setProperty("http.nonProxyHosts", "localhost|127.0.0.1");
    

    【讨论】:

      【解决方案3】:

      如果您使用 HTTPS 连接网络服务,那么要设置的代理属性是

      https.proxyHost
      https.proxyPort
      

      (http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html)

      【讨论】:

      • 引用是正确的 - 它甚至包括错字“htttps.proxyHost”:-)
      • 应该是https,不是htttps?
      【解决方案4】:

      除了设置系统属性外,还使用 ​​java.net.Authenticator 设置代理配置。

      final String authUser = "user";
      final String authPassword = "password";
      Authenticator.setDefault(
         new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(
                     authUser, authPassword.toCharArray());
            }
         }
      );
      
      System.setProperty("http.proxyUser", authUser);
      System.setProperty("http.proxyPassword", authPassword);
      

      【讨论】:

        【解决方案5】:

        我能够使用以下代码通过代理。

        在 Snehasish 代码中添加了一些新行

        final String authUser = "username";
        final String authPassword = "password";
        Authenticator.setDefault(
           new Authenticator() {
              public PasswordAuthentication getPasswordAuthentication() {
                 return new PasswordAuthentication(
                       authUser, authPassword.toCharArray());
              }
           }
        );
        url = new URL("http://www.somewebsite.com/sendmyrequest");
        
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setFollowRedirects(true);
        
        System.getProperties().put("http.proxyHost", "proxy.xyz.com");
        System.getProperties().put("http.proxyPort", "portNumber");
        System.setProperty("http.proxyUser", authUser);
        System.setProperty("http.proxyPassword", authPassword);
        System.getProperties().put("http.proxySet", "true");
        

        【讨论】:

          【解决方案6】:

          您可以使用System.setProperty()设置代理

          System.setProperty("http.proxyHost","122.183.139.107");
          System.setProperty("http.proxyPort","8080");
          

          如果你想删除

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

          【讨论】:

            【解决方案7】:

            我遇到了同样的问题。只有以下代码适用于我设置代理。

            System.setProperty("java.net.useSystemProxies", "true");
            

            【讨论】:

              【解决方案8】:

              没有http.proxySet这样的属性。

              您需要在使用任何 HTTP URL 之前设置其他属性,之后更改它们没有任何效果。

              如果您需要动态更改代理,请参阅 java.net.Proxy。

              '明文连接?'意思就是它所说的:您正在将 SSL 用于非 SSL 目标,可能是纯文本目标。

              【讨论】:

              • 非 SSL 目标您的意思是说 Web 服务是非 SSL 的?代理服务器是否可能正在与 Web 服务进行纯文本通信?我的端点显然在使用 HTTPS
              • 对其他答案的评论(或不同的 cmets)比另一个答案更多。
              【解决方案9】:

              在 Eclipse IDE 中转到 Window->Preferences。在左边的小方框里写proxy。您应该看到 Network Connections,输入您将使用的请求的代理设置(HTTP 应该足够)。我相信这将解决您的问题,而无需在代码本身中设置代理。

              【讨论】:

              • 太棒了。他在生产中做什么?没有答案。
              • 代理服务器端是否存在阻止我与 Web 服务通信的东西?还是我的 java 代码有问题?
              • @EJP 他应该把这些告诉公司或公司负责网络和代理权限的系统人员,这样他们就会让机器或整个网络访问那个 url,即我在一家使用代理进行外部网络访问的保险公司工作时所做的事情。这不是程序员在代码本身中写下代理设置的责任,我认为这是一种糟糕的编程习惯。你能告诉我当他们更改代理IP,用户名或密码时他会做什么,他会更改代码并重新编译吗?
              • @Kunal 最有可能是代理服务器,我说这是一个浪费一整天时间访问网络服务的人,而我在大三的时候不知道公司有代理服务器程序员。在开发程序时,我建议按照我在 Eclipse IDE 中所说的去做,当你完成程序时,与公司的网络管理员或在那里设置代理服务器的人交谈,并要求必要的权限绕过代理来访问该 Web 服务的 url。至少我所做的并且会再次做的:)
              • @Panda 所以这应该是你的答案,以及那些员工实际做了什么。
              猜你喜欢
              • 2015-08-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多