-1

听起来很奇怪,但这种方法适用于 get/大多数 POST 请求,但有时(通常最近)StringBuffer 不会捕获附加在 http 请求 (JSON) 末尾的数据。我哪里错了?

/**
     * Parses a client request and calls the appropriate handler
     * @throws Exception
     */
    private void processClientRequest() throws Exception{

        //Get the client request
        BufferedReader clientRequest = new BufferedReader(new InputStreamReader(connectedClient.getInputStream()));

        //Start response object
        DataOutputStream responseToClient = new DataOutputStream(connectedClient.getOutputStream());

        System.out.println(clientRequest);
        String requestString = clientRequest.readLine();

        String header = requestString;

        //Break up request
        StringTokenizer tokenizer = new StringTokenizer(header);

        //Different request parts
        String httpMethod = tokenizer.nextToken();
        String httpQueryString = tokenizer.nextToken();

        //Print client request
        StringBuffer responseBuffer = new StringBuffer();
        while (clientRequest.ready()) {
            responseBuffer.append(requestString + " ");
            System.out.println(requestString);

            requestString = clientRequest.readLine();
        }

        //Process GET request
        if (httpMethod.equals("GET")) {
            processGETRequests(httpQueryString, requestString);


        }else if (httpMethod.equals("POST")) {
            processPOSTRequests(responseBuffer, httpQueryString);
        }
    }
4

1 回答 1

1

ready()仔细阅读文档。

我认为你的问题是:

while (clientRequest.ready()) {

如果流还没有数据(如果客户端发送的速度比您的服务器处理的慢),那么您不会等待客户端发送它。

于 2013-07-03T02:17:06.613 回答