Hi,

I want to get the information from the 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
}
catch(Exception ex)
{
	System.out.println(ex);
}

}

}

but ,i have an exception

java.lang.ClassCastException: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream cannot be cast to java.lang.String


how can i retrieve data from the website?
If any have the working code pls show me


thank you in advance

The problem appears to be in (String)yahooConnection.getContent() Check the API doc for this method - it returns an Object, in this case its an HttpInputStream object) and that kind of object cannot be simply cast to a String. (In fact it's not even a public class).
You should probably be using yahooConnection.getInputStream(); which returns an input stream from which you can read the lines that have been obtained from the URL.

ps In future, please post your code with code=java tags, and show the exact line that the Exception refers to

import java.net.*;
import java.io.*;
public class WebSiteReader {
  public static void main(String args[]){
       String nextLine;
       URL url = null;
       URLConnection urlConn = null;
       InputStreamReader  inStream = null;
       BufferedReader buff = null;
       try{
          // Create the URL obect that points
          // at the default file index.html
          url  = new URL("http://www.yahoo.com" );
          urlConn = url.openConnection();
         inStream = new InputStreamReader( 
                           urlConn.getInputStream());
           buff= new BufferedReader(inStream);
        
       // Read and print the lines from index.html
        while (true){
            nextLine =buff.readLine();  
            if (nextLine !=null){
                System.out.println(nextLine); 
            }
            else{
               break;
            } 
        }
     } catch(MalformedURLException e){
       System.out.println("Please check the URL:" + 
                                           e.toString() );
     } catch(IOException  e1){
      System.out.println("Can't read  from the Internet: "+ 
                                          e1.toString() ); 
  }
 }
}

If you can't be bothered to post your code properly formatted in code tags (as I requested before) and without any indication as to what your question might be... then I can't be bothered to read it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.