• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

URLConnection ???-urgent please help

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic