java 读取页面,java 读取页面源码 的多种方式

以下都是实战经验:

1、Socket读取

String strServer=http://www.google.cn;//这里同样可以用ip来访问:203.208.35.100

String strPage="/language_tools?hl=zh-CN";

try {

String hostname = strServer;

int port = 80;

InetAddress addr = InetAddress.getByName(hostname);

Socket socket = new Socket(addr, port);

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());

}

2、HttpClient方式

HttpClient client=new HttpClient();

GetMethod method=new GetMethod("http://www.baidu.com/");

int status=client.executeMethod(method);

if(status==HttpStatus.SC_OK){

//读取内容

byte[] responseBody = method.getResponseBody();

//处理内容

System.out.println(new String(responseBody));

System.out.println("文件名称:"+method.getPath());

}

3、HttpURLConnection方式

URL url = new URL("这里是你要连接的地址");

HttpURLConnection conn = (HttpURLConnection)url.openConnection();

conn.setDoOutput(true);//是否可用于输出(输出参数),默认为fasle。另:setDoInput()为是否可用于输入,默认为true

String parameters = "name=admin&password=123456";//这里是要传递的参数

OutputStream os = conn.getOutputStream();

os.write(parameters.getBytes("utf-8"));

os.flush();

os.close();

System.out.println("返回状态码:"+conn.getResponseCode());

System.out.println("返回消息:"+conn.getResponseMessage());

InputStream is = conn.getInputStream();

BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));

String line = null;

while((line=br.readLine())!=null){

System.out.println(line);

}

//  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();;

//  DocumentBuilder db = dbf.newDocumentBuilder();

//  Document doc = db.parse(is);

如果谁还有更多的方式分享,请留言!

posted on 2008-11-12 10:12 筱 筱 阅读(878) 评论(2)  编辑  收藏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值