|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
Features Reading Data from the Internet
Lesson 6, Java Basics
By: Yakov Fain
Jan. 26, 2006 12:00 AM
At first, create an instance of the class URL: try{
URL xyz = new URL("http://www.xyz.com:80/training.html");
}
catch(MalformedURLException e){
e.printStackTrace();
}
The MalformedURLException could be thrown if a non-valid URL has been used, for example missed protocol if you forgot to start URL with http://, extra spaces, etc. The MalformedURLException does not indicate that the remote machine has problems - just check the spelling of the URL. Creation of the URL object does not establish the connection with the remote machine: you'll still need to open a stream to read it. Usually you have to perform the following steps to read a file from the Internet: Step 1. Create and instance of the class URL Step 2. Create an instance of the class URLConnection and open a connection using the URL instance from step 1. Step 3. Get a reference to an input stream of this object by calling the method URLConnection.getInputStream() Step 4. Read the data from the stream (use the buffered reader to speed up the process). Since the streams from the package java.io are being used here for the read/write operations, you'll have to handle I/O exceptions. The server you are trying to connect to has to be up and running and, in case of using http protocol, the special software (Web Server) has to be "listening to" the port that you specified in the URL instance. By default, Web servers are listening to the port number 80. The program below reads and prints on the system console the content of the file index.html from yahoo.com. Obviously, to test this program your computer has to be connected to the Internet. 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() );
}
}
}
The class WebSiteReader explicitly creates the URLConnection object. Strictly speaking we could get away just with the class URL: URL url = new URL("http://www.yahoo.com");
InputStream in = url.getInputStream();
Buff= new BufferedReader(new InputStreamReader(in));
The reason why you may consider using the URLConnection class is that it could give you some additional control over the I/O process. For example, by calling its method setDoInput(true) you could allow (or disallow) downloads.
Connecting Through HTTP Proxy ServersMost of the companies use firewalls for security reasons and their employees reach the Internet through the HTTP proxy server. Check the settings of your Internet browser to find out the host name and port number of the proxy server. If you are using Microsoft Internet Explorer check the menu Internet Options | Connections | LAN Setting. Netscape Navigator has proxy settings under Preferences | Advanced | Proxies. While Java Applets know parameters of the proxy servers because they live inside the browser that has the proper settings, Java applications should set the parameters of the proxy server, for example: System.setProperty("http.proxyHost","xyz.com");
System.setProperty("http.proxyPort", 8080);
If you do not want to hardcode these value, pass them to the program from the command line: c:\practice>java -Dhttp.proxyHost=xyz.com -Dhttp.proxyPort=8080 WebSiteReader If you run this program without specifying the proxy settings, you'll get the UnknownHostException.
How to Download Files From the InternetBy using the class URL with input streams, we should be able to download practically any file (images, music, binary files) from the unsecured Internet site. The trick is in proper opening of the file stream. Let‚s write the class FileDownload that takes a URL and the file name as a command line arguments and copies remote file on your local disk. import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.DataInputStream;
import java.net.URL;
import java.net.URLConnection;
class FileDownloader{
public static void main(String args[]){
if (args.length!=2){
System.out.println(
"Proper Usage: java FileDownloader RemoteFileURL LocalFileName");
System.exit(0);
}
DataInputStream in=null;
DataOutputStream out=null;
FileOutputStream fOut=null;
try{
URL remoteFile=new URL(args[0]);
URLConnection fileStream=remoteFile.openConnection();
// Open the input streams for the remote file
fOut=new FileOutputStream(args[1]);
// Open the output streams for saving this file on disk
out=new DataOutputStream(fOut);
in=new DataInputStream(fileStream.getInputStream());
// Read the remote on save save the file
int data;
while((data=in.read())!=-1){
fOut.write(data);
}
System.out.println("Download of " + args[0] + " is complete." );
} catch (Exception e){
e.printStackTrace();
} finally {
try{
in.close();
fOut.flush();
fOut.close();
} catch(Exception e){e.printStackTrace();}
}
}
}
The Stock Quote ProgramIn this section we'll write the program that can read stock market quotes from the Internet. There are many Internet sites providing stock market quotes, and 20 minutes delayed quotes are free. Wall Street companies subscribe for the real-time market data feed. One of the popular Internet sites is Yahoo and the URL for getting stock prices is http://finance.yahoo.com Point your Web browser to this site and get the price quote of any stock symbol. Note the URL of the resulting Web page in your browser. For example, if you've selected the symbol IBM, the URL would look like this: http://finance.yahoo.com/q?s=IBM Right click on this page and select the View Source from the popup menu to see the HTML contents of this page: you'll see lots of HTML tags and the information about the IBM's trading will be buried somewhere deep inside the file: ... Last Trade:</TD><TD class=yfnc_tabledata1><BIG><B>95.32</B> ... The next step is to modify the URL in our class WebSiteReader to print the content of the page about the symbol IBM: url = new URL("http://finance.yahoo.com/q?s=IBM");
You can also store the whole page in a Java String variable instead of printing it. Just change the while loop to look as follows: String theWholePage;
while (txt =buff.readLine() != null ){
theWholePage=theWholePage + txt;
}
If you add some smart tokenizing of theWholePage to get rid of all HTML tags and everything but Last Trade value, you can create your own little GUI Stock Quote screen. While this approach is useful to sharpen you tokenizing skills, it may not be the best solution, especially if Yahoo will change the wording of this page. That's why we'll be using another Yahoo's URL that provide stock quotes in a cleaner comma separated values format (CSV). Here's the URL that should be used for the IBM's symbol: http://quote.yahoo.com/d/quotes.csv?s=IBM&f=sl1d1t1c1ohgv&e=.csvThis URL would produce a string that looks something like this (the price quotes are not real): "IBM",95.32,"1/16/2004","5:01pm",+1.30,95.00,95.35,94.71,9305000The next class StockQuoter prints the price quote for the symbol that is specified as a command line argument. import java.net.*;
import java.io.*;
import java.util.StringTokenizer;
public class StockQuoter {
String csvString;
URL url = null;
URLConnection urlConn = null;
InputStreamReader inStream = null;
BufferedReader buff = null;
StockQuoter(String symbol){
try{
url = new
URL("http://quote.yahoo.com/d/quotes.csv?s="
+ symbol + "&f=sl1d1t1c1ohgv&e=.csv" );
urlConn = url.openConnection();
inStream = new
InputStreamReader(urlConn.getInputStream());
BufferedReader buff= new BufferedReader(inStream);
// get the quote as a csv string
csvString =buff.readLine();
// parse the csv string
StringTokenizer tokenizer = new
StringTokenizer(csvString, ",");
String ticker = tokenizer.nextToken();
String price = tokenizer.nextToken();
String tradeDate = tokenizer.nextToken();
String tradeTime = tokenizer.nextToken();
System.out.println("Symbol: " + ticker +
" Price: " + price + " Date: " + tradeDate
+ " Time: " + tradeTime);
} catch(MalformedURLException e){
System.out.println("Please check the spelling of the URL:"
+ e.toString() );
} catch(IOException e1){
System.out.println("Can't read from the Internet: " +
e1.toString() );
}
finally{
try{
inStream.close();
buff.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String args[]){
if (args.length==0){
System.out.println(
"Sample Usage: java StockQuoter IBM");
System.exit(0);
}
StockQuoter sq = new StockQuoter(args[0]);
}
}
To see the latest price of IBM stock start the StockQuoter program as follows:
c:\practice>java StockQuoter IBM In this lesson I tried to show you that working with the streams over the net may be as simple as dealing with files on your local disk. These days Java runs remote-controlled Mars rovers, and this is not a rocket science anymore - just open a Java stream that points at Mars. Reader Feedback: Page 1 of 2
Your Feedback
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||