问题
I'm using Jersey HTTP Client for sending POST request and receiving 411 Content Length missing in response. However this error is not thrown in all the cases. There are some calls where I'm using json content type and it is working fine.
For this particular request where I'm sending the request to a specific URL I'm receiving 411 Content Length required. While with the same payload when I'm using a different URL it is working fine.
This is how I'm setting Form Entity in Jersey Client Request
public static MultivaluedMap<String, String> getMultivaluedMapRequest(Map<String, String> map) {
MultivaluedMap<String, String> bodyParams = new MultivaluedHashMap<String, String>();
if (null != map && map.size() > 0) {
for (Entry<String, String> param : map.entrySet()) {
bodyParams.add(param.getKey(), param.getValue());
}
}
return bodyParams;
}
private static Entity<?> getRequestEntity(final HttpRequestPayload<?> payload) {
if (payload.getEntity() instanceof MultivaluedMap<?, ?>) {
return Entity.form((MultivaluedMap<String, String>) payload.getEntity());
} else {
return Entity.entity(payload.getEntity(), payload.getMediaTypeProduced());
}
}
This is how the call is made
final Entity<?> entity = getRequestEntity(payload);
Response response = target.request(payload.getMediaTypeConsumed()).headers(payload.getHeaders()).post(entity);
I'm not sure what the issue is. I think it has something to do with the URL I'm sending the request to.
回答1:
HttpClientConnectionManager has the ClientProperties.REQUEST_ENTITY_PROCESSING set to CHUNKED by default. As a solution I set it to BUFFERED and it worked.
ClientConfig configuration = new ClientConfig(); configuration.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED);
来源:https://stackoverflow.com/questions/37142850/jersey-http-client-replying-with-411-on-post-request