2010-12-22 28 views
1

我如何使用套接字从我的java程序发送多个http请求。其实我已经试过:在java中使用套接字的多个HTTP请求

import java.net.*; 
import java.io.*; 
class htmlPageFetch{ 
    public static void main(String[] args){ 
     try{ 
      Socket s = new Socket("127.0.0.1", 80); 
      DataInputStream dIn = new DataInputStream(s.getInputStream()); 
      PrintWriter dOut = new PrintWriter(s.getOutputStream(), true); 
      dOut.println("GET /mytesting/justCheck.html HTTP/1.1\r\nHost:localhost\r\n\r\n"); 
      boolean more_data = true; 
      String str; 
      int i = 0; 
      while(more_data){ 
       str = dIn.readLine(); 
       if(str==null){ 
        //Now server has stopped sending data    //So now write again the inputs 
        dOut.println("GET /mytesting/justCheck1.html HTTP/1.1\r\nHost:localhost\r\n\r\n");       


        continue; 
       } 
       System.out.println(str); 
      } 
     }catch(IOException e){ 

     } 
    } 
} 

但是当我再次发送请求时,它没有处理? 提前致谢。

+0

你不应该捕获一个异常,并且不要做任何事情。在你的例外中,至少打印stacktrace(e.printStackTrace())来查看是否有错误。 – Joel 2010-12-22 14:35:55

+1

你是否必须使用套接字来做到这一点? HTTP在几个库中被抽象出来,可以清理一些。 – Nicholas 2010-12-22 14:49:13

回答

相关问题