Jersey Http 客户端在 POST 请求上回复 411 [英] Jersey Http Client replying with 411 on POST request

查看:30
本文介绍了Jersey Http 客户端在 POST 请求上回复 411的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Jersey HTTP 客户端发送 POST 请求并接收响应中缺少 411 内容长度.但是,并非在所有情况下都会抛出此错误.在某些调用中,我使用了 json 内容类型,并且工作正常.

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.

对于这个特定的请求,我将请求发送到特定的 URL,我收到 411 Content Length required.虽然当我使用不同的 URL 时使用相同的有效负载,但它工作正常.

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.

这就是我在 Jersey 客户端请求中设置表单实体的方式

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());
    }
}

这就是调用的方式

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.

推荐答案

HttpClientConnectionManager 默认将 ClientProperties.REQUEST_ENTITY_PROCESSING 设置为 CHUNKED.作为一个解决方案,我将它设置为 BUFFERED 并且它起作用了.

HttpClientConnectionManager has the ClientProperties.REQUEST_ENTITY_PROCESSING set to CHUNKED by default. As a solution I set it to BUFFERED and it worked.

ClientConfig 配置 = new ClientConfig();configuration.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED);

ClientConfig configuration = new ClientConfig(); configuration.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED);

这篇关于Jersey Http 客户端在 POST 请求上回复 411的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆