This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Accessing Internet through Proxy Server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Accessing Internet through Proxy Server" Watch "Accessing Internet through Proxy Server" New topic

Accessing Internet through Proxy Server

Sajid Niazi

Joined: Aug 15, 2002
Posts: 26
When I access internet through my java application from my home i.e. no proxy server, then I am able to get access to it.
But when I try to access internet from my office where there is a proxy server, I get a unkownhostexception and none of the java applications making using of package execute.
Please help me find out the solution to this problem.
// sample code
public class SourceViewer
public static void main(String args[])
URL url;
if (args.length > 0)
url = new URL(args[0]);
url = new URL("");
URLConnection uc = url.openConnection();
InputStream raw = uc.getInputStream();
InputStream buffer = new BufferedInputStream (raw);

Reader r = new InputStreamReader(buffer);
int c;
while ((c = != -1)
System.out.print((char) c);
catch(MalformedURLException e)
System.err.println("Invalid URL");
catch(IOException e)

}// main

}// class SView
Dan Richardson

Joined: Aug 06, 2002
Posts: 23
Try out the following code. Replace the your_proxy stuff with your actual proxy server and port and proxy login and password. Hope this helps.
Sajid Niazi

Joined: Aug 15, 2002
Posts: 26
Thanx Dan Richardson. Your solution worked for me.
It even worked when I commented out the code that deals with username and password. Probably 'coz I had access to internet when I used a browser(setting proxies in browser).
Again a lot of thanx for helping me.
sajid niazi
[ August 17, 2002: Message edited by: Sajid Niazi ]
It is sorta covered in the JavaRanch Style Guide.
subject: Accessing Internet through Proxy Server
Similar Threads
proxy server in java
How to parse xml inpustream?
File upload
Help OO'ing my code please...
Data type mapping issue