File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "" Watch "" New topic

Prasad Sonawane

Joined: Jul 28, 2008
Posts: 9
hi all...
I am stuck with a problem. In my application i am trying to connect to
the web sites using URL and URLConnection classes of package.
I want to connect to the website through the URL provided and then i am trying to read the contents of the web page. The code is like this :
URL url = new URL("");
URLConnection conn = url.openConnection();

System.out.println("URL connected..");
catch(UnknownHostException unknown){
System.out.println("Exception while connecting :");

int character;
int contentLength = conn.getContentLength();
System.out.println("contentLength = "+contentLength);

if(contentLength > 0){
InputStream in = conn.getInputStream();
while((character = != -1){

Here above when i use conn.connect() it gives me
(I have also tried using the class HttpURLConnection instead of URLConnection. But still it gives this exception..)
I am worried why this is happening even if i provide a proper URL.
Can some one have solution for this ?

Thanks in advance..

Regards ~
Prasad Sonawane, Member Technical Staff, CDAC, India.
vipin jos

Joined: Nov 18, 2008
Posts: 24

Are you working from behind a proxy server?
Can you try the same to an intranet URL and let us know the result?

Joined: Jun 12, 2007
Posts: 6
I have the same issue and behind a Proxy server.
at Source)
at Source)
at Source)
at Source)
at Source)
at Source)
at<init>(Unknown Source)
at Source)
at Source)
at Source)
at Source)
at Source)
at ScagntJavaHttp.httpMakeRequest(
at test.main(

Added the following lines of code in order to take care of the proxy server

After adding the above lines of code, I now get ;

Response Code : 405
Response Message : Method Not Allowed

I tried using POST and GET methods. Both return the same result.
I use HttpURLConnection class .

Is there something else that needs to be taken care of ?
Runnig the code on Eclipse in win XP.

I agree. Here's the link:
It's not a secret anymore!