aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes java.net.UnknownHostException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "java.net.UnknownHostException" Watch "java.net.UnknownHostException" New topic
Author

java.net.UnknownHostException

Prasad Sonawane
Greenhorn

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 java.net 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("http://www.yahoo.com");
URLConnection conn = url.openConnection();

try{
conn.connect();
System.out.println("URL connected..");
}
catch(UnknownHostException unknown){
System.out.println("Exception while connecting :");
unknown.printStackTrace();
}

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

if(contentLength > 0){
InputStream in = conn.getInputStream();
while((character = in.read()) != -1){
System.out.println((char)character);
}
in.close();
}
/**************************************************************/

Here above when i use conn.connect() it gives me java.net.UnknownHostException.
(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
Greenhorn

Joined: Nov 18, 2008
Posts: 24
Hi,

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

Joined: Jun 12, 2007
Posts: 6
I have the same issue and behind a Proxy server.

java.net.UnknownHostException: google.com
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at ScagntJavaHttp.httpMakeRequest(ScagntJavaHttp.java:185)
at test.main(test.java:23)


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: http://aspose.com/file-tools
 
subject: java.net.UnknownHostException