3

我现在正在编写一个 http 服务器,但从套接字读取时遇到问题。我的问题是inputStream来自客户端的消息永远不会结束,它会一直读取直到客户端关闭。我知道客户端在发送 http 请求后并没有立即关闭与服务器的连接。while loop当客户端发送所有请求数据(即标头+正文)时, 如何退出。

while (in.hasNextLine()) {
    String line = in.nextLine();

    if (line.equals("")){ // last line of request header is blank
        break; // quit while loop when last line of header is reached
    } else {
        request = request + line + "\n";
    }
}

在阅读了你们的评论和回答后,这就是我想出的,

     is = incoming.getInputStream();
            os = incoming.getOutputStream();
            in = new Scanner(is);
            out = new DataOutputStream(os);

            RequestHandler rh = new RequestHandler();
            int length = 0;
            while (in.hasNextLine()) {
                String line = in.nextLine();
                if (line.equals("")) { // last line of request message
                                        // header is a
                                        // blank line
                    break; // quit while loop when last line of header is
                            // reached
                }

                if (line.startsWith("Content-Length: ")) { // get the
                                                            // content-length
                    int index = line.indexOf(':') + 1;
                    String len = line.substring(index).trim();
                    length = Integer.parseInt(len);
                }

                request = request + line + "\n";
            }

             byte[] body = new byte[length];
             int i = 0;
             while (i < length) {
             byte b = in.nextByte();
             body[i] = b;
             i++;
             }

但是,我仍然不明白按字节读取。我可以编写我的代码来读取直到-1,但是当没有 EOF 并且客户端没有关闭连接时仍然卡住。

4

3 回答 3

6

There are 3 ways of detecting the end of the stream depending on what requests you are handling:

  1. If it is a GET or HEAD request, you only need to read the HTTP headers, request body is normally ignored if it exists, so when you encounter \r\n\r\n, you reach the end of the request(actually the request headers).
  2. If it is a POST method, read the Content-Length in the header and read up to Content-Length bytes.
  3. If it is a POST method and the Content-Length header is absent, which is most likely to happen, read until -1 is returned, which is the signal of EOF.
于 2012-11-13T02:09:18.877 回答
4

I've got it :) Thank you guys for comments and answers...

     is = incoming.getInputStream(); // initiating inputStream
            os = incoming.getOutputStream(); // initiating outputStream

            in = new BufferedReader(new InputStreamReader(is)); // initiating
                                                                // bufferReader
            out = new DataOutputStream(os); // initiating DataOutputSteream

            RequestHandler rh = new RequestHandler(); // create a
                                                        // requestHandler
                                                        // object

            String line;
            while ((line = in.readLine()) != null) {
                if (line.equals("")) { // last line of request message
                                        // header is a
                                        // blank line (\r\n\r\n)
                    break; // quit while loop when last line of header is
                            // reached
                }

                // checking line if it has information about Content-Length
                // weather it has message body or not
                if (line.startsWith("Content-Length: ")) { // get the
                                                            // content-length
                    int index = line.indexOf(':') + 1;
                    String len = line.substring(index).trim();
                    length = Integer.parseInt(len);
                }

                request.append(line + "\n"); // append the request
            } // end of while to read headers

            // if there is Message body, go in to this loop
            if (length > 0) {
                int read;
                while ((read = in.read()) != -1) {
                    body.append((char) read);
                    if (body.length() == length)
                        break;
                }
            }

            request.append(body); // adding the body to request
于 2012-11-13T16:37:47.163 回答
0

I want to share my code that works great:

private static String getClientRequest(Socket client) throws IOException, SocketException {
    System.out.println("Debug: got new client " + client.toString());
    BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8"));

    StringBuilder requestBuilder = new StringBuilder();
    String line;
    int contectLegnth = 0;
    
    while (!(line = br.readLine()).isBlank()) {
        requestBuilder.append(line + "\r\n");
        if (line.toLowerCase().startsWith("content-length")) {
            contectLegnth = Integer.parseInt(line.split(":")[1].trim());
        }
    }
    

    StringBuilder requestBodyBuilder = new StringBuilder();
    if (contectLegnth > 0) {
        int read;
        while ((read = br.read()) != -1) {
            requestBodyBuilder.append((char) read);
            if (requestBodyBuilder.length() == contectLegnth)
                break;
        }
        requestBuilder.append("\r\n" + requestBodyBuilder);
    }
   
    return requestBuilder.toString();
}
于 2020-11-18T22:36:55.360 回答