【问题标题】:Jersey client 1.17 PUT request returns Error 411. The request must be chunked or have a content lengthJersey 客户端 1.17 PUT 请求返回错误 411。请求必须分块或具有内容长度
【发布时间】:2015-08-21 09:13:32
【问题描述】:

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

HTTP客户端

    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 中有效。我不知道我的泽西客户有什么问题。有人遇到过类似的情况吗?

【问题讨论】:

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


    【解决方案1】:

    所以我弄清楚了问题所在。在 Jersey 客户端中,要向 PUT 请求发送一个空正文,我们需要将一个空字符串设置为您的客户端的实体。比如

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

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

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      我不明白为什么 Azure API 不起作用 equals。 它适用于不同的地方。

      示例:

      ... 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);
      

      【讨论】:

        猜你喜欢
        • 2017-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-12
        • 2011-12-08
        • 1970-01-01
        • 2013-03-15
        • 1970-01-01
        相关资源
        最近更新 更多