Jersey客户端1.17 PUT请求返回错误411.请求必须分块或具有内容长度

时间:2015-08-21 09:13:33

标签: azure request put jersey-client http-error

我正在尝试在Jersey 1.17中创建一个空的PUT请求,以便像这样与Azure Storage REST API进行通信

了HTTPClient

    ClientConfig config = new DefaultClientConfig();

    config.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT,
            connectionTimeOut);
    config.getProperties().put(ClientConfig.PROPERTY_READ_TIMEOUT,
            readTimeOut);

    Client client = Client.create(config);
    client.addFilter(new LoggingFilter());
    WebResource webResource = client.resource(
                    "https://" + accountName + "." + storageMedium + "."
                            + host)
                            .path(containerName)
            .queryParam("restype", "container");

PUT请求

   //
    ClientResponse response = webResource
                    .header(Constants.X_MS_VERSION, "2015-02-21")
                    .header("x-ms-date", storage.date)
                    .header("Authorization", authorizationString).put(ClientResponse.class);

服务器的响应是

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Length Required</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Length Required</h2>
<hr><p>HTTP Error 411. The request must be chunked or have a content length.</p>
</BODY></HTML>

我认为问题不在于Azure REST api,因为PUT请求适用于Postman。我不知道我的Jersey客户端有什么问题。有人遇到过类似的情况吗?

2 个答案:

答案 0 :(得分:2)

所以我弄清楚问题是什么。在Jersey客户端发送一个空体到PUT请求时,我们需要将一个空字符串设置为客户端的实体。例如

resource.put(ClientResponse.class, "");

这将在http请求中将Content-Length设置为0,将Content-Type设置为“text / plain”。

希望这有帮助!

答案 1 :(得分:0)

我不明白为什么Azure API无法正常工作。 它适用于不同的地方。

<强>示例:

... post(String.class, "");
... post(String.class, "{}");
... post(String.class, new byte[]{});

您也可以使用 ClientResponse 类处理响应,例如 response.getStatus()

ClientResponse response = ... post(ClientResponse.class, new byte[]{});
String ret = response.getEntity(String.class);
相关问题