通过java.net.Socket类抓取网页内容

作者:网络 来源:佚名 更新时间:2008-07-07 11:22:49 点击:

通过 java.net.socket 类访问一个web页面,并且返回结果

import java.io.*;
import java.net.*;

public class firstsocket
{
public static void main(string args[])
{

//判断参数是否为两个,正确的用法为 firstsocket 网站 页面
if(args.length!=2)
{
system.out.println("usage : firstsocket host page\r\nexample:firstsocket www.google.com /");
system.exit(0);
}
string strserver=args[0]; //取得第一个参数
string strpage = args[1]; //取得第二个参数

try
{
string hostname = strserver;
int port = 80;
inetaddress addr = inetaddress.getbyname(hostname);
socket socket = new socket(addr, port); //建立一个socket

//发送命令
bufferedwriter wr = new bufferedwriter(new outputstreamwriter(socket.getoutputstream(), "utf8"));
wr.write("get " + strpage + " http/1.0\r\n");
wr.write("host:" + strserver + "\r\n");
wr.write("\r\n");
wr.flush();

//接收返回的结果
bufferedreader rd = new bufferedreader(new inputstreamreader(socket.getinputstream()));
string line;
while ((line = rd.readline()) != null) {
system.out.println(line);
}
wr.close();
rd.close();
}
catch (exception e)
{
system.out.println(e.tostring());
}
}
}