2012-11-23 31 views
1

我有Java套接字的另一个问题。服务器的套接字不会像应该那样解释请求。我认为读取HTML标题后的行有问题,但我不知道这段代码有什么问题。 TIA。阅读请求暂停服务器

这里的代码片段:

@Override 
public void run() { 
    DataOutputStream dout = null; 
    BufferedReader reader = null; 
    try { 
     dout = new DataOutputStream(socket.getOutputStream()); 
     reader = new BufferedReader(
       new InputStreamReader(
         socket.getInputStream(), UTF-8")); 

     String requestString = reader.readLine(); 
     StringTokenizer tokenizer = new StringTokenizer(requestString); 
     String httpMethod = tokenizer.nextToken(); 
     String httpQueryString = tokenizer.nextToken(); 

     System.out.println("method: " + httpMethod); 
     System.out.println("query: " + httpQueryString); 
     String line; 
     int i = 0; 
     while (! (line = reader.readLine()) 
       .equals("")) { 
      System.out.println(i++ + " : " + line); 
     } 

     //DEBUG 
     System.out.println("foo"); 
    // HERE IS THE PROBLEM !!! 
     line = reader.readLine(); 
     System.out.println("aaa " + line); 
     line = reader.readLine(); 
     System.out.println("bbb " + line); 
     line = reader.readLine(); 
     System.out.println("ccc " + line); 

     // Pseudocode 
     if (GET) { 
      if ("/") { 
       ... 
      } else if (isFile) { 
       ... 
      } else { 
       ... 
      } 
     } else if (POST) { 
      ... //TODO 
     } else { 
      Error 404 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     //Cleaning 
     try { 
      reader.close(); 
      dout.close(); 
      socket.close(); 
     } catch (IOException e) { 
      Logger.getAnonymousLogger().warning("Socket cannot be closed"); 
     } 
    } 
} 

和输出我得到:

INFO: Server is RUNNING 
INFO: Connection accepted 
method: GET 
query:/
0 : Host: 127.0.0.1:8001 
1 : User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0 
2 : Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
3 : Accept-Language: pl,en-us;q=0.7,en;q=0.3 
4 : Accept-Encoding: gzip, deflate 
5 : DNT: 1 
6 : Connection: keep-alive 
7 : Cache-Control: max-age=0 
foo 

--- in this place server halts --- 
--- then I refresh page or do anything else that sends request (GET, POST) --- 
--- and server receives 'remaining' part of the request --- 

aaa null // in POST this line has send values 
bbb null 
ccc null 
INFO: method = GET 
INFO: Connection accepted 
method: GET 
query:/
0 : Host: 127.0.0.1:8001 
1 : User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0 
2 : Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
3 : Accept-Language: pl,en-us;q=0.7,en;q=0.3 
4 : Accept-Encoding: gzip, deflate 
5 : DNT: 1 
6 : Connection: keep-alive 
foo 

--- in this place server halts --- 
--- then I refresh page or do anything else that sends request (GET, POST) --- 
--- and server receives 'remaining' part of the request --- 

回答

0

这是请求没有剩余部分。 GET请求在空行之后停止。服务器被阻塞,因为没有传入数据(直到下一个请求)

+0

是的,但我该如何解决这个问题?应该是: 1.接收来自客户端的请求。 2.回答此请求。 3.接收剩余部分。 4.做些什么。 ? – Miki

+0

请勿自行实施HTTP服务器。使用servlet或其他东西。 – irreputable

+0

我必须。这是编程课的一个项目。 我想,如果我想要得到其余的标题(或POST消息体),我必须发送响应“200 OK”。但是,那么,我怎样才能发回带有搜索结果的页面呢? – Miki