I am trying to get HTTP response with the help of apache httpclient. I get headers successfully but it throws exception when I try to get contents. Exception is:
Caused by: org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 42 320; received: 7 787)
[INFO] [talledLocalContainer] at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:178) ~[httpcore-4.4.15.jar:4.4.15]
[INFO] [talledLocalContainer] at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:135) ~[httpclient-4.5.13.jar:4.5.13]
[INFO] [talledLocalContainer] at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:148) ~[httpclient-4.5.13.jar:4.5.13]
[INFO] [talledLocalContainer] at com.blueway.common.utils.BWFileUtils.writeToFile(BWFileUtils.java:207) ~[engine-module-common-0.0.1-SNAPSHOT.jar:?]
[INFO] [talledLocalContainer] at com.blueway.platform.app.engine.technicalconnector.impl.service.azureblobstorage.AzureBlobStorageService.getBlob(AzureBlobStorageService.java:49) ~[web-0.0.1-SNAPSHOT.jar:?]
[INFO] [talledLocalContainer] at com.blueway.platform.app.engine.technicalconnector.impl.service.azureblobstorage.invoker.AzureBlobStorageGetBlobInvoker.invoke(AzureBlobStorageGetBlobInvoker.java:22) ~[web-0.0.1-SNAPSHOT.jar:?]
[INFO] [talledLocalContainer] at com.blueway.platform.app.engine.technicalconnector.impl.service.azureblobstorage.AzureBlobStorageInvokeWrapper.doInvoke(AzureBlobStorageInvokeWrapper.java:34) ~[web-0.0.1-SNAPSHOT.jar:?]
[INFO] [talledLocalContainer] at com.blueway.engine52.support.ConnectorAzureBlobStorage.invoke(ConnectorAzureBlobStorage.java:46) ~[web-0.0.1-SNAPSHOT.jar:?]
[INFO] [talledLocalContainer] at com.blueway.engine52.service.instruction.ConnectorHandlingInstruction.invokeConnector(ConnectorHandlingInstruction.java:68) ~[web-0.0.1-SNAPSHOT.jar:?]
[INFO] [talledLocalContainer] at com.blueway.engine52.service.instruction.ConnectorHandlingInstruction.executeInstructionWithSupport(ConnectorHandlingInstruction.java:50) ~[web-0.0.1-SNAPSHOT.jar:?]
[INFO] [talledLocalContainer] at com.blueway.engine52.service.instruction.ConnectorHandlingInstruction.executeInstructionWithSupport(ConnectorHandlingInstruction.java:19) ~[web-0.0.1-SNAPSHOT.jar:?]
[INFO] [talledLocalContainer] at com.blueway.engine52.service.instruction.SupportHandlingInstruction.executeInstruction(SupportHandlingInstruction.java:99) ~[web-0.0.1-SNAPSHOT.jar:?]
[INFO] [talledLocalContainer] at com.blueway.engine52.service.instruction.Instruction.tryExecuteInstruction(Instruction.java:363) ~[web-0.0.1-SNAPSHOT.jar:?]
and the code is
public CloseableHttpResponse get(final String containerName, final String blobName) throws AzBException{
final Logger logger = LoggerFactory.getLogger(getClass());
final String url = HTTPS + accountName + END_POINT + containerName + "/" + blobName;
try (final CloseableHttpClient httpClient = HttpClients.createDefault()) {
final HttpGet httpGet = new HttpGet(url);
logger.debug("Sending HTTP GET request to URL: {}", url);
final CloseableHttpResponse response = httpClient.execute(httpGet);
logger.debug("Received HTTP response with status code: {}", response.getStatusLine().getStatusCode());
return response;
} catch (IOException e) {
throw new AzBException("An.get HttpGet error: " + e.getCause().getMessage(), e);
}
}
any help will be appreciated. thanks
Do you have success with implementing this requirement using the Http classes packaged with Java? Sorry, it's just... I'm not personally a fan of Apache HttpClient. The last time I used it it was extremely unstable, bugs across different versions, and I just ended up abandoning it and implementing something similar myself.
You can easily do a Http Get with Java as follows:
Check out [this article] for more details. In older code that implements something similar you might see the use of the HttpURLConnection class. The above code snippet uses the Java 11 classes that support HTTP/2 and Web Socket.
Sorry for not answering your question, I'm just not a fan of Apache HttpClient. And hopefully you can agree that the implementation given above is already quite succinct and easy to understand so one doesn't really need an external library.
The creation of Apache HttpClient precedes the release of the Java 11 classes used above and were probably implemented to abstract developers away from the complexity of the now deprecated HttpURLConnection.
Do to the problems I've encountered working with these things I have also abstracted the implementation into a wrapper. So my client code (business logic code) uses the wrapper and doesn't get directly linked to the classes of the implementation I'm actually using, whether that be Apache HttpClient or Java.
So in my code you might see:
Where
HttpResponseandHttpGetis my own classes. So the client code doesn't know what implementation is being used and it doesn't link the implementation to the classes of that implementation. So if I ever need to refactor the implementation, then I can just refactor the implementation, and not need to refactor all my client code as well.