11

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 that request is not getting through. Same proxy settings are working fine on SoapUI. Please find below the system properties set by me.

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 program throws an exception as java.net.UnknownHostException:

What is it I am missing?

5
  • 2
    What are you doing with your "newProps" after that?
    – Claudio
    Jan 10, 2013 at 3:57
  • Which host is unknown? Proxy or WebService server? Jan 10, 2013 at 4:01
  • I set that to system properties. btw I am also trying a different proxy where i am getting a different exception as, javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    – Kunal
    Jan 10, 2013 at 4:01
  • Webservice server was unknown.
    – Kunal
    Jan 10, 2013 at 4:03
  • Is your web service running over https? If yes, then try to set https proxy configurations. props.put("https.proxyHost", "10.x.x.x"); props.put("https.proxyPort", "80"); Apr 24, 2014 at 14:28

9 Answers 9

9
java -Dhttp.proxyHost=proxyhostURL
     -Dhttp.proxyPort=proxyPortNumber
     -Dhttp.proxyUser=someUserName
     -Dhttp.proxyPassword=somePassword javaClassToRun

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

6

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
6

if you are connecting webservice using HTTPS then the proxy property to set is

https.proxyHost
https.proxyPort

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

2
  • 2
    The citation is correct - and it even includes the typo "htttps.proxyHost" :-)
    – oberlies
    Aug 8, 2013 at 14:37
  • 1
    Surely that should be https, not htttps?
    – JMK
    Nov 7, 2016 at 13:18
2

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);
2

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");
2

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","");
2

I had the same problem. Just the following code works for me for setting proxy.

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

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.

2
  • Non-SSL target you mean to say the Web Serice is non-SSL? Is it possible that the proxy server is talking plain-text to webservice? My endpoint is clearly using HTTPS
    – Kunal
    Jan 11, 2013 at 7:01
  • More a comment (or different comments) on other answers than another answer. Dec 11, 2018 at 9:43
-5

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.

6
  • 3
    Great. What does he do in production? Not an answer.
    – user207421
    Jan 10, 2013 at 21:14
  • Is there something at proxy server end which is preventing me talk to webservice? Or its a problem in my java code?
    – Kunal
    Jan 11, 2013 at 7:06
  • @EJP he should talk these to the system staff of the company or the firm which are responsible of the network and proxy permissions, so they will let the machine or entire network to access that url, that is what I did when worked a insurance company that used proxy for outer network access. This is not the programmers responsibility to write down the proxy settings inside the code itself and I think it is a poor programming practice. Can you tell me what will he do when they change the proxy IP, username or password will he change the code and compile it again?
    – Panda
    Jan 11, 2013 at 8:38
  • @Kunal there it is the proxy server most probably, I am saying this as a person who wasted entire day to access a web service without knowing there was a proxy server at the firm when I was a junior programmer. While developing the program I suggest do what I said inside eclipse IDE, and when you finished the program talk to the network admin of the firm or whoever set that proxy server there and demand necessary permissions bypass proxy to access the url of that web service. At least that what I did and would do again :)
    – Panda
    Jan 11, 2013 at 8:47
  • @Panda So that should have been your answer, along with what those staff actually did.
    – user207421
    Jan 11, 2013 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.