public class NasdaqQuotes {
static final String surl = "http://quotes.nasdaq.com/quote.dll?page=xml&mode=stock";
public static void main(String[] args) {
try {
NasdaqQuotes nq = new NasdaqQuotes();
System.out.println(nq.getQuote(args[0]));
} catch (Exception e) {}
}
public String getQuote(String symbol) throws Exception {
String nurl = this.surl + "&symbol=" + symbol;
URL url = new URL(nurl);
return doGet(url);
}
private String doGet(URL url) throws Exception {
URLConnection conn = null;
InputStream in = null;
String results = null;
conn = url.openConnection();
byte[] buffer = new byte[0];
byte[] chunk = new byte[4096];
int count;
in = conn.getInputStream();
while ((count = in.read(chunk)) > 0) {
byte[] t = new byte[buffer.length + count];
System.arraycopy(buffer, 0, t, 0, buffer.length);
System.arraycopy(chunk, 0, t, buffer.length, count);
buffer = t;
}
results = new String(buffer);
return results;
}
}
|
