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 dont set the above system variables, you will most likely get the familiar java.net.UnknownHostException.
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.