0

我们使用 CXF JAX-RS 客户端向我方执行 PUT 请求。请求正文为空。一个简单的请求调用会导致服务器响应代码为 411。

Response-Code: 411  
"Content-Length is missing"

我们党的 REST-server 需要设置 Content-Length HTTP-header。

我们根据有关分块的说明关闭了分块,但这并没有解决问题。REST 服务器仍然回答 411 错误。

这是来自 cxf.xml 文件的管道配置

    <http-conf:conduit name="{http://myhost.com/ChangePassword}WebClient.http-conduit">
        <http-conf:client AllowChunking="false"/>
    </http-conf:conduit>

日志中的行确认我们的请求的执行绑定到我们的管道配置:

DEBUG o.a.cxf.transport.http.HTTPConduit - Conduit '{http://myhost.com/ChangePassword}WebClient.http-conduit' has been configured for plain http.

显式添加 Content-Length 标头也无济于事。

    Invocation.Builder builder = ...  
    builder = builder.header(HttpHeaders.CONTENT_LENGTH, 0);

CXF 客户端的日志条目确认了标头设置,但是当我们嗅探数据包时,我们惊讶地发现标头设置已被 CXF 客户端完全忽略。未发送 Content-Length 标头。

这是日志。存在 Content-Length 标头:

INFO  o.a.c.i.LoggingOutInterceptor - Outbound Message
---------------------------
ID: 1
Address: http://myhost.com/ChangePassword?username=abc%40gmail.com&oldPassword=qwerty123&newPassword=321ytrewq
Http-Method: PUT
Content-Type: application/x-www-form-urlencoded
Headers: {Accept=[application/json], client_id=[abcdefg1234567890abcdefg12345678], Content-Length=[0], Content-Type=[application/x-www-form-urlencoded], Cache-Control=[no-cache], Connection=[Keep-Alive]}
--------------------------------------
DEBUG o.apache.cxf.transport.http.Headers - Accept: application/json
DEBUG o.apache.cxf.transport.http.Headers - client_id: abcdefg1234567890abcdefg12345678
DEBUG o.apache.cxf.transport.http.Headers - Content-Length: 0
DEBUG o.apache.cxf.transport.http.Headers - Content-Type: application/x-www-form-urlencoded
DEBUG o.apache.cxf.transport.http.Headers - Cache-Control: no-cache
DEBUG o.apache.cxf.transport.http.Headers - Connection: Keep-Alive

这是数据包嗅探器的输出。Content-Length 标头不存在:

PUT http://myhost.com/ChangePassword?username=abc%40gmail.com&oldPassword=qwerty123&newPassword=321ytrewq HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Accept: application/json
client_id: abcdefg1234567890abcdefg12345678
Cache-Control: no-cache
User-Agent: Apache-CXF/3.1.8
Pragma: no-cache
Host: myhost.com
Proxy-Connection: keep-alive

有谁知道实际上如何禁用分块?

这是我们的代码:

    public static void main(String[] args)  
    {  
        String clientId   = "abcdefg1234567890abcdefg12345678";  
        String uri        = "http://myhost.com";  
        String user       = "abc@gmail.com";  

        Client client = ClientBuilder.newBuilder().newClient();  
        WebTarget target = client.target(uri);  
        target = target.path("ChangePassword").queryParam("username", user).queryParam("oldPassword", "qwerty123").queryParam("newPassword", "321ytrewq");  
        Invocation.Builder builder = target.request("application/json").header("client_id", clientId).header(HttpHeaders.CONTENT_LENGTH, 0);  
        Response response = builder.put(Entity.form(new Form()));  
        String body = response.readEntity(String.class);  
        System.out.println(body);  
    }

版本:

  • 操作系统:Windows 7 企业版 SP1
  • 拱门:x86_64
  • 爪哇:1.7.0_80
  • CXF:3.1.8
4

1 回答 1

1

我有一个非常相似的问题,我无法像您通过尝试关闭分块来解决。

我最终做的是将 Content-Length 设置为 1 并添加一些空格“”作为正文。对我来说,似乎服务器应用程序之前的代理服务器被拒绝了请求,这样做让我通过了代理服务器,并且服务器能够处理请求,因为它仅基于 URL 运行。

于 2016-12-15T08:59:56.710 回答