浏览 3095 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-10-09
只能抓取前面一个网页 后面复用的不行 public class text { public static void main(String[] args) throws IOException { String hostname = "www.iteye.com"; int port = 80; InetAddress addr = InetAddress.getByName(hostname); System.out.println(addr); Socket s = new Socket(hostname, port); // s.setSoTimeout(99999); s.setKeepAlive(true); if(s.getKeepAlive()) { System.out.println("******************keep-alive*****************"); } BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); wr.write("GET / HTTP/1.1" + "\r\n" + "Host: www.iteye.com" + "\r\n" + "Connection: Keep-Alive" + "User-Agent: tse" + "\r\n"); wr.write("\r\n"); wr.flush(); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); String line; while((line = br.readLine()) != null) { System.out.println(line); } System.out.println(s.isClosed()); //以下想用复用同一个HOST+port的socket,可是不行 wr.write("GET /search HTTP/1.0" + "\r\n" + "Host: www.iteye.com" + "\r\n" + "Connection: Keep-Alive" + "User-Agent: tse" + "\r\n"); // wr.write("GET /"); // wr.write("HOST:" + hostname + "\r\n"); wr.write("\r\n"); wr.flush(); BufferedReader b = new BufferedReader(new InputStreamReader(s.getInputStream())); String linee; if(s.getInputStream().available() > 0) { System.out.println("hava something"); } while((linee = b.readLine()) != null) { System.out.println(linee); } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |