Proxy settings in a java program

前端 未结 9 667
[愿得一人]
[愿得一人] 2020-12-14 12:09

I am trying to connect to a web service with a client generated from wsdl through a java program in eclipse. I am passing my request through a proxy server. But it seems tha

相关标签:
9条回答
  • 2020-12-14 12:25

    I was able to get through the proxy by using the following piece of code.

    Added some new lines to Snehasish Code

    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");
    
    0 讨论(0)
  • 2020-12-14 12:29

    Apart from setting system properties use java.net.Authenticator to set proxy configuration too.

    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);
    
    0 讨论(0)
  • 2020-12-14 12:31

    There is no such property as http.proxySet.

    You need to set the other properties before using any HTTP URLs, and changing them afterwards has no effect.

    If you need to change proxies dynamically, see java.net.Proxy.

    'Plaintext connection?' means exactly what it says: you are using SSL to a non-SSL target, probably a plaintext one.

    0 讨论(0)
  • 2020-12-14 12:34

    I use the following code (and it works):

        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");
    
    0 讨论(0)
  • 2020-12-14 12:38

    In eclipse IDE go to Window->Preferences. There write proxy to the little box on the left side. You should see Network Connections, there input the proxy setting for the requests (HTTP should be sufficent) you will use. I believe that will solve you problem without setting the proxy inside the code itself.

    0 讨论(0)
  • 2020-12-14 12:42

    You can set Proxy using System.setProperty()

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

    and if you want to remove

    System.setProperty("http.proxyHost","");
    System.setProperty("http.proxyPort","");
    
    0 讨论(0)
提交回复
热议问题