码迷,mamicode.com
首页 > 其他好文 > 详细

socket获取百度页面

时间:2015-01-04 14:59:22      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
public class BaiduServer {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub

System.out.println(InetAddress.getByName("www.baidu.com"));
Socket s = new Socket(InetAddress.getByName("www.baidu.com"), 80);
if (s.isConnected()) {
System.out.println(s.getInetAddress());
System.out.println("客户端已经连上");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
out.println("GET / HTTP/1.1");
out.println("Host: www.baidu.com:80");
out.println("\r\n");

String line = "";

while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

}

 

技术交流群:194469050

socket获取百度页面

标签:

原文地址:http://www.cnblogs.com/xinanheishao/p/4200880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!