Periodically incomplete response using Apache HttpClient 3.0.1

I am stumped about this, so I thought I would ask if any of you meet this, since developing HttpClient is a bit of art.

The problem I encountered is this: the application uses the Apache HttpClient Java library to communicate with a server on the same company network. Most of the time it works without problems, but sometimes we will see a flurry of exceptions caused by incomplete answers: they miss the last three characters of the closing tag, so the complainant in the client complains. It lasts 5 to 10 minutes and then leaves.

I was unable to replicate this problem locally, and I confirmed that the answer is completely written by the server. The client receives the response content using the postMethod getResponseBodyAsStream () method, but it only calls once. Maybe he needs to quote the call of this method until it becomes zero for the rare case when the reaction is buffered?

I will be grateful for any input.

Edit: the server writes the content length header and performs the cleanup correctly, and on the client the data is read into a line with the following parameters:

//method is a PostMethod, client is a HttpClient
client.executeMethod(hostconfig, method); 

InputStream is = method.getResponseBodyAsStream();
String response = null;

try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();    
    byte[] buf = new byte[1024];
    int len;

    while ((len = is.read(buf)) > 0) {
        bos.write(buf, 0, len);
    }

    response = new String(bos.toByteArray(), "UTF-8");

} ... // closing try block
+3
source share
2 answers

? 100%, Commons-HttpClient , . - , getResponseBodyAsStream.

, . , , , , ? ( ).

, ... Commons HttpClient - .

+1

. URL- localhost .

...

"", , Thread.sleep(1000), . , , . ( , , read() , , , , , , , ). , ...

- readLine() BufferedReader. . readLine, , .

.

+1

Source: https://habr.com/ru/post/1714092/


All Articles