200 OK: Request succeeded. The requested resource can be found later in this message.
301 Moved Permanently: Requested resource has moved. New location is specified later in this message.
400 Bad Request: Request message is not understood by the server.
404 Not Found: Requested document is not found on this server.
关于 HTTP 和所有返回代码的信息可以在 HTTP 1.1 规范 RFC2616 中找到。
public class ReadURL1 {
public static void main(String argv[]) throws Exception {
final int HTTP_PORT = 80;
if(argv.length != 1) {
System.out.println("Usage: java ReadURL1 <url>");
System.exit(0);
}
Socket socket = new Socket(argv[0], HTTP_PORT);
BufferedWriter out
= new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader in
= new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.write("GET /index.html HTTP/1.0\n\n");
out.flush();
public class Stock {
private static String name, time, price;
// Given a quote from the server,
// retrieve the name, price, and date of the stock
public static void parse(String data) {
int index = data.indexOf('"');
name = data.substring(++index,(index = data.indexOf('"', index)));
index +=3;
time = data.substring(index, (index = data.indexOf('-', index))-1);
index +=5;
price = data.substring(index, (index = data.indexOf('>', index)));
}
// Get the name of the stock from
public static String getName(String record) {
parse(record);
return(name);
}
// Get the price of the stock from
public static String getPrice(String record) {
parse(record);
return(price);
}
// Get the date of the stock
public static String getDate(String record) {
parse(record);;
return time;
}
}
StockReader.java 类
这个类的任务是连接到 Yahoo Quote 服务器,并从服务器上获取股票行情。它使用 Stock 类解析从服务器返回的字符串。示例代码 4 是它的一个实现。