File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.net.UnknownHostException

 
Prasad Sonawane
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
vipin jos
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic