advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   TIP BANK
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
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....

It's quick, easy and you get access to all the articles on DevX.
This registration/login is to allow you to read articles on devx.com.
Already a member?





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