This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

URLConnection ???-urgent please help  RSS feed

 
sunilkumar ssuparasmul
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I tried a small sample of code using URLConnection. i just want
to download a page using it.
For eg
URL theURL = new URL("http://www.yahoo.com/");
URLConnection theUC = theURL.openConnection();
theUC.connect();
this cod e does not compile even though i tried to catch the exception . it says UnknownHostException .
but instead of yahoo.com if i give localhost it compiles fine.
Can somebody please explain how 2 connect 2 a remote server like yahoo and download a pagefrom it.
U rhelp will be much appreciated
Thanks in advance
sunil.s
------------------
"Winners don't do different things
They do things differently"
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Well I tried to simulate u r error / bug by the following code but I couldnt simulate the same. The Code compiled perfectly.

Sorry if I wasnt of much help.
All de best all de same.
Regds
Gautham Kasinath
U R Not Alone
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused by your exception. UnknownHostException is a Runtime Exception and not a compiler error. Are you positive thats the error you are receiving? I also ran the following:

Give this a try.
Hope this helps
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the URL you must specify the complete locator.
ie http://www.yahoo.com/index.html
Following code will display the contents(html code) of index page
when u run at command prompt(Net Connected).
Suppose u want to save it in a file, then open a FileWriter object and stored the contents in it.

class Display
{
public static void main(String ss[])
{
try{
URL url=new URL("http://www.yahoo.com/index.html");
BufferedReader buf=new BufferedReader(new InputStreamReader(url.openStream()));
String s;
while((s=buf.readLine())!=null)
{
System.out.println(s);
}
}catch(Exception e)
{
System.out.println("Unknown Host or Page Not Found");
}
}
}

ssat2k@yahoo.com
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!