• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

How to Call a web service from behind a firewall/Proxy

 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do I need a different code when I need to call a web service from behind a firewall/Proxy..... Usually in corporate offices we are behind the firewall/Proxy server.

Do I need to pass the proxy settings before calling the web service.

If yes, how do we do that? Any clues?
 
Greenhorn
Posts: 6
IBM DB2 Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yogesh,

the easiest way may be to start your java client using the -D options for the proxy server.

-Dhttps.proxyHost=
-Dhttps.proxyPort=
-Dhttp.proxyHost=
-Dhttp.proxyPort=
 
Yogesh Gandhi
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is the case when we are using a desktop application.

In my case, My class is being executed as a thread along with a web-application.
The thread gets started as soon as the server of the web-application starts.

Now in this case, I don't know how can I give -D option. Can I?

 
David Nicholls
Greenhorn
Posts: 6
IBM DB2 Eclipse IDE VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again,

in this case start your servlet container using these flags.

What is your environment? Are you using eclipse? Using tomcat?

Anyway. You may just as well alter your code:
Just put the following static initializer block in your thread class (or any class that is loaded before calling the service).

static {
System.setProperty("https.proxyHost", "IPgoesHere");
System.setProperty("https.proxyPort", "PortGoesHere");
System.setProperty("http.proxyHost", "IPgoesHere");
System.setProperty("http.proxyPort", "PortGoesHere");
}

I recommend starting the JVM using the -D options. Then you don't have to change your code.
 
Yogesh Gandhi
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Nicholls wrote:Hi again,

in this case start your servlet container using these flags.

What is your environment? Are you using eclipse? Using tomcat?

Anyway. You may just as well alter your code:
Just put the following static initializer block in your thread class (or any class that is loaded before calling the service).

static {
System.setProperty("https.proxyHost", "IPgoesHere");
System.setProperty("https.proxyPort", "PortGoesHere");
System.setProperty("http.proxyHost", "IPgoesHere");
System.setProperty("http.proxyPort", "PortGoesHere");
}

I recommend starting the JVM using the -D options. Then you don't have to change your code.



So we are sure on this that if I use -D option I dont need to change my code?

Well in another scenario (Where I had a main program java code)  where I was calling httpURL.openConnection()
I had to use overloaded method httpURL.openConnection(proxy)

and pass a Proxy object in order to make it work. Even though I had it passed as JVM arguments to the main program, this openConnection call still required the proxy object in order to work correctly.
 
Saloon Keeper
Posts: 28714
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "-D" option on the java command line is equivalent to calling System.setProperty() before invoking the main() method.

So a good practice might look like this:


That allows you to code default proxy settings in the application. If, however, you  needed to talk via an alternative proxy server, you could then override on the command line:
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic