diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/RequestProtocolCompliance.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/RequestProtocolCompliance.java index a50dc8cac..f8815fc76 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/RequestProtocolCompliance.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/RequestProtocolCompliance.java @@ -32,7 +32,9 @@ import java.util.List; import org.apache.http.Header; import org.apache.http.HeaderElement; +import org.apache.http.HttpEntity; import org.apache.http.HttpEntityEnclosingRequest; +import org.apache.http.HttpHeaders; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; @@ -43,8 +45,8 @@ import org.apache.http.annotation.ThreadingBehavior; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.cache.HeaderConstants; import org.apache.http.client.methods.HttpRequestWrapper; -import org.apache.http.entity.AbstractHttpEntity; import org.apache.http.entity.ContentType; +import org.apache.http.entity.HttpEntityWrapper; import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHttpResponse; import org.apache.http.message.BasicStatusLine; @@ -193,9 +195,17 @@ class RequestProtocolCompliance { } private void addContentTypeHeaderIfMissing(final HttpEntityEnclosingRequest request) { - if (request.getEntity().getContentType() == null) { - ((AbstractHttpEntity) request.getEntity()).setContentType( - ContentType.APPLICATION_OCTET_STREAM.getMimeType()); + final HttpEntity entity = request.getEntity(); + if (entity != null && entity.getContentType() == null) { + final HttpEntityWrapper entityWrapper = new HttpEntityWrapper(entity) { + + @Override + public Header getContentType() { + return new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_OCTET_STREAM.getMimeType()); + } + + }; + request.setEntity(entityWrapper); } } diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolDeviations.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolDeviations.java index 53d0d665e..cdccb78a1 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolDeviations.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolDeviations.java @@ -42,9 +42,9 @@ import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpExecutionAware; import org.apache.http.client.methods.HttpRequestWrapper; import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.client.utils.DateUtils; import org.apache.http.conn.routing.HttpRoute; import org.apache.http.entity.ByteArrayEntity; -import org.apache.http.client.utils.DateUtils; import org.apache.http.impl.execchain.ClientExecChain; import org.apache.http.message.BasicHttpEntityEnclosingRequest; import org.apache.http.message.BasicHttpRequest;