10

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?

Charles Menguy
  • 40,830
  • 17
  • 95
  • 117
Kunal
  • 111
  • 1
  • 1
  • 4

9 Answers9

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/

Abiram
  • 171
  • 1
  • 4
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");
Donato Szilagyi
  • 4,279
  • 4
  • 36
  • 53
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)

Guillaume Barré
  • 4,168
  • 2
  • 27
  • 50
Jt0
  • 99
  • 2
  • 4
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);
Snehasish
  • 1,051
  • 5
  • 20
  • 37
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");
Jack
  • 2,891
  • 11
  • 48
  • 65
jai kiran
  • 21
  • 1
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","");
Bhola
  • 392
  • 1
  • 15
2

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

System.setProperty("java.net.useSystemProxies", "true");
BlackBeard
  • 10,246
  • 7
  • 52
  • 62
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.

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
user207421
  • 305,947
  • 44
  • 307
  • 483
  • 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 '13 at 07:01
  • More a comment (or different comments) on other answers than another answer. – Kalle Richter Dec 11 '18 at 09: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.

Panda
  • 21
  • 3
  • 3
    Great. What does he do in production? Not an answer. – user207421 Jan 10 '13 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 '13 at 07: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 '13 at 08: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 '13 at 08:47
  • @Panda So that should have been your answer, along with what those staff actually did. – user207421 Jan 11 '13 at 22:51
  • @EJP Yea sorry I was in a hurry at the moment :) – Panda Jan 14 '13 at 09:25