jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Can I get the information from the Website?(Please take look) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can I get the information from the Website?(Please take look)" Watch "Can I get the information from the Website?(Please take look)" New topic
Author

Can I get the information from the Website?(Please take look)

Anu Sri
Ranch Hand

Joined: Jul 29, 2002
Posts: 43
Hi All
I am trying to get information from the site and store into the database.
Here is my code..folowing.. I am notgetting where it goes wrong..
import java.io.*;
import java.net.*;
import java.util.*;
public class DownloadWebPage
{
public static void main(String[] args)
{

System.out.println("Hello World!");
try{
//www.gwbush.org // security exception
//URL hp = new URL("http://localhost:8080" );
//http://quotes.freerealtime.com/dl/frt/M?IM=quotes&symbol=F&type=Quote
PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("Html.html")),true);
int c;
//URL hp = new URL("http://localhost:8080/examples/servlets/helloworld.html" );
URL hp = new URL("http://quotes.freerealtime.com/dl/frt/M?IM=quotes&symbol=F&type=Quote" );
System.out.println("Protocol :=" +hp.getProtocol());
System.out.println("Host:=" +hp.getHost());
System.out.println("Port:=" +hp.getPort());
System.out.println("File :=" +hp.getFile());
System.out.println("Ext :=" +hp.toExternalForm());
URLConnection hpCon = hp.openConnection();
System.out.println("Date :=" +new Date(hpCon.getDate()));
System.out.println("ContentType :=" +hpCon.getContentType());
System.out.println("Expires :=" +hpCon.getExpiration());
System.out.println("Last Modified :=" +new Date(hpCon.getLastModified()));
int len = hpCon.getContentLength();
System.out.println("ContentLength :="+len);
System.out.println("Permission :="+(pCon.getPermission()).toString());
System.out.println("Permission :="+(pCon.getPermission()).getName());
StringBuffer sb= new StringBuffer();
if ( len > 0 ){
System.out.println("====Content =====");
InputStream input = hpCon.getInputStream();
int i = len;
while (((c = input.read()) != -1) && ( --i > 0 )) {

//System.out.println((char)c);
sb.append((char)c);

}
String st = new String(sb);
pw.println(st);
input.close();
}else {
System.out.println("No Content Available");
}
}
catch (Exception e)
{
System.out.println(e);e.printStackTrace();
}
}
}
Please let you if you find any thing wrong in above code and post correct code.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Well, what happens when you try to compile it??
you'll notice that two of your lines are bad:

there's no variable called pCon it should be hpCon
Once I changed that -- everything ran fine. The URL that you've hardcoded in: http://quotes.freerealtime.com/dl/frt/M?IM=quotes&symbol=F&type=Quote returns a content-length of '-1' so your code prints "No Content Available" just like its supposed to.
If you substitute that URL for one that doesn't give a -1, like http://www.hpmiddleware.com/SaIsapi.dll/SaServletEngine.class/default.jsp . Then everything works fine too.
* Check out the UBB [CODE] tags they preserve the whitespace in your code and make it easier for others to read. *
Anu Sri
Ranch Hand

Joined: Jul 29, 2002
Posts: 43
Thank you Jessica. It works good now..
Thank you once again
Anupama
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Can I get the information from the Website?(Please take look)
 
Similar Threads
Need some basic help with converting
J2ME, servlet and database help..
Urgent Need some basic help with converting
Socket constructor error
How to save html output of jsp into a file