BnJ
BnJ

Reputation: 1044

Apache HttpClient not executing third request

I'm using Apache HttpClient to do a multipart request to upload a file, but it doesn't work after the third request.

Here is my code :

HttpClient httpClient = HttpClientBuilder.create().build();

Map<String, String> requestParams = new HashMap<>();
requestParams.put("param1", "myrequestparam");

String url = UPLOAD_URL + "?param1=" + myRequestParam;

HttpEntity entity = MultipartEntityBuilder.create()
                    .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                    .addBinaryBody("file", file, ContentType.MULTIPART_FORM_DATA, "file")
                    .build();

HttpPost request = new HttpPost(url);
request.setEntity(entity);

try {

    httpClient.execute(request);

} catch (IOException e) {
    throw new InternalServerErrorException(e);
}

It is executing multiple times, works fines the two firsts times, but nothing happen the third time.

What am I doing wrong ?

Upvotes: 1

Views: 487

Answers (2)

Simon Logic
Simon Logic

Reputation: 418

I faced this issue today on foreign project which was claimed as 'working'. Here is a fix:

try (CloseableHttpResponse response = httpClient.execute(request)) {
  ...
}

Upvotes: 0

Thomas Refflinghaus
Thomas Refflinghaus

Reputation: 96

Try to call

EntityUtils.consume(entity)

at the end of your code to close your request. Than start the next post. doc

Or you can read the tutorial pdf

Upvotes: 3

Related Questions