读取请求暂停服务器

时间:2012-11-23 22:21:40

标签: java http sockets

我有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 ---

1 个答案:

答案 0 :(得分:0)

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