java - 读取请求使服务器停止

标签 java http sockets

我在 Java 套接字方面还有另一个问题。服务器的套接字没有按照应有的方式解释请求。我认为读取 HTML header 之后的行有问题,但我不知道这段代码有什么问题。 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 ---

最佳答案

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

关于java - 读取请求使服务器停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13536347/

相关文章:

java - 监听tomcat realm认证事件

javascript - 单页应用程序中模板检索的困惑以及与典型文件检索的区别

java - 如何为以 3 个数字或连字符结尾的字符串编写正则表达式模式?

python - 检查 Python 请求库是否正在下载文件

sockets - 如何格式化 HTTP 响应

python - 套接字接收缓冲区大小

c# - 通过 HTTP 302 重定向从 WebClient 获取位置?

ios - 从 Swift 中的 Socket 获取响应

java - 在我的例子中如何使用replaceFirst()来获取一系列连续的字符串?

java - Groovy 检查未使用的导入