Gerard Davison is a Senior Principal Software Engineer working at Oracle in the UK on SOAP and REST tooling. Currently he is contributing in the area of WADL generation and client generation in the Jersey project and is maintaining the Abbot swing automation project. He also maintain a small holding of Hudson nodes run all those tests. He graduated from the University of Reading with a degree in Human Cybernetic and can't help looking for feedback loops. Gerard is a DZone MVB and is not an employee of DZone and has posted 32 posts at DZone. You can read more from them at their website. View Full User Profile

Proxying a Request to Localhost in Java

02.21.2013
| 3626 views |
  • submit to reddit

It can be very convenient when developing to server based application to run them using "localhost" in order to maintain consistency between developer machines. This is normally a good idea but there is a small case where this can cause problems.

Consider if you are running a local http proxy on your machine in order to capture your HTTP traffic. (Cough perhaps even the one in JDeveloper I work on). Then you might run into Java bug 6737819. Basically by default JDK 1.6 was hard coded not to send any request to localhost via a proxy which of course was a bit of a pain. Luckily a workaround was put in where you could put the string "~localhost" in your nonProxyHosts entry to turn of this feature:

java -client -classpath classes -Dhttp.proxyHost=localhost 
-Dhttp.proxyPort=8099 -Dhttp.nonProxyHosts=~localhost 
-Dhttps.proxyHost=localhost 
-Dhttps.proxyPort=8099 client.Example 

Now moving forward to JDK 1.7 this workaround no longer works; but you need to take care to define nonProxyHosts as an empty string:

 java -client -classpath classes -Dhttp.proxyHost=localhost -Dhttp.proxyPort=8099
 -Dhttp.nonProxyHosts= -Dhttps.proxyHost=localhost 
-Dhttps.proxyPort=8099 client.Example  

If you define this any anything other than an empty string the DefaultProxySelector though beware because internally it will append / or use the http.nonProxyHosts value from ../jre/lib/net.properties".

Just a minor complication that is not obvious from the published API.
 

Published at DZone with permission of Gerard Davison, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Alosh Bennett replied on Thu, 2013/02/21 - 10:47am

 Or, you could map the domain to localhost in your /etc/hosts file.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.