facebook

Error while Fetching data from a website

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #291429 Reply

    Pradeep_J
    Member

    Hi ,
    My aim is to retrieve data from a website using HttpURLConnection
    I wrote this

    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;

    public class webPractice
    {

    public static void main(String[] args)
    {

    try
    {
    URL yahoo = new URL(“http://www.yahoo.com/”);
    URLConnection yahooConnection = (URLConnection) yahoo.openConnection();
    yahooConnection.connect();
    long s=yahooConnection.getDate();

    System.out.println(“date”+(String)yahooConnection.getContent());
    System.out.println(“Hi”);
    }
    catch (MalformedURLException e)

    {
    System.out.println(“URL Exception”);
    // new URL() failed
    }
    catch (IOException es)
    {
    System.out.println(“IO Exception”);
    es.printStackTrace();
    // openConnection() failed
    }

    }

    }

    It is giving the exception as

    java.net.UnknownHostException: http://www.yahoo.com
    IO Exception
    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 webPractice.main(webPractice.java:19)

    How can I retrieve the data from a website?
    If do u have any working code plz show me….

    #291451 Reply

    Loyal Water
    Member

    Moving to Off Topic >> Software Development.

    #291472 Reply

    rmcvay
    Member

    Something like this maybe.

    public StringBuffer getYahoo() throws IOException, MalformedURLException
    {
        URL url = new URL("http://www.yahoo.com");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.connect();
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuffer inBuff = new StringBuffer();
    
        int c;
        while ((c = br.read()) != -1)
        {
            inBuff.append((char)c);
        }
        return inBuff;
    }
    
Viewing 3 posts - 1 through 3 (of 3 total)
Reply To: Error while Fetching data from a website

You must be logged in to post in the forum log in