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
Tip of the Day
Rate this item | 0 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.
advertisement
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers