advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
Average Rating: 3.2/5 | Rate this item | 12 users have rated this item.
Expertise: Intermediate
Language: Java
July 2, 2001
Setting Proxy in Java Code

The java.net package contains classes that deal with connections across the network. If there is a direct connection to the Internet via a modem, various classes like the HttpConnection or the raw Connection class can be used to get a connection to a server on the net.

This approach fails if you are going through a proxy server. To tell the JVM a proxy is installed, system variables need to be set explicitly set using the System class.

What you have to do is basically set three variables. These are:
 
    proxySet
    proxyPort
    proxyHost

This small piece of code shows how to set a proxy before calling the connecting code:
 
//some previous code

    System.getProperties().put(“proxySet”,”true”);
    System.getProperties().put(“proxyPort”,”8080”);
    System.getProperties().put(“proxyHost”,”host”);
    try
    {
          URL validateURL = new URL(“www.foo.com?name=Irfan”);
          URLConnection connection = validateURL.openConnection();
          //Do some thing with the connection

    }
    catch(Exception e)
    {
        //Show the Exception
    }

//Rest of the stuff....

If the connection is being rooted through a proxy and you don’t set the above system variables, you will most likely get the familiar java.net.UnknownHostException.
Irfan Ahmed
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
Please rate this item (5=best)
 1  2  3  4  5
advertisement
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About