获得“411长度要求”在来自HTTP客户端的PUT请求之后 [英] Getting "411 Length Required" after a PUT request from HTTP Client

查看:789
本文介绍了获得“411长度要求”在来自HTTP客户端的PUT请求之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个实现HTTP客户端的Java程序。
我测试它向服务器发送请求。 GET,POST和DELETE请求正常工作。
例如在POST请求之后我得到一个输出

 提取的数据:
{status: {message:ok}}

,数据库反映所做的更改。



在PUT请求之后,我得到以下网页的html标记,表示错误。

 提取的数据:
< html>
< head>< title> 411所需长度< / title>< / head>
< body bgcolor =white>
< center>< h1> 411所需长度< / h1>< / center>
< hr>< center> nginx / 1.2.6< / center>
< / body>
< / html>

因此数据库中没有任何变化。



<我发现这可能与Content-Length标题有关,但我不确定。
尝试添加此标头后,我的程序等待一分钟然后抛出异常,通知它无法处理服务器响应。



我也可以如果需要,提供任何代码或堆栈跟踪。

解决方案

是的,与 Content-Length <相关的问题/ code>。 HTTP错误411表示


服务器拒绝接受没有定义Content-Length的请求。如果客户端在请求消息中添加了包含消息正文长度的有效Content-Length头字段,则客户端可以重复该请求。


所以当你在POST / PUT方法中发送一个空的 RequestBody 时,你还需要发送 Content-Length:0 。因此,请在您的请求中添加此标头。我不认为这个标题会导致您添加到Request Object中的问题。


I am working on a Java program that implements an HTTP Client. I test it sending requests to a server. GET, POST and DELETE requests work ok. For example after a POST request I get an output

Data extracted:
{"status":{"message":"ok"}}

and the database reflects the changes made.

After a PUT request, however I get the following html markup of a webpage indicating an error.

Data extracted:
<html>
<head><title>411 Length Required</title></head>
<body bgcolor="white">
<center><h1>411 Length Required</h1></center>
<hr><center>nginx/1.2.6</center>
</body>
</html>

and accordingly no changes in the database.

I found that this can have something to do with the Content-Length header, but I'm not sure. After trying to add this header my program waits for a minute and then throws an exception informing that it couldn't handle the server response.

I can also provide any code or stack trace if needed.

解决方案

Yes, the issue related to Content-Length. HTTP Error 411 means

The server refuses to accept the request without a defined Content- Length. The client MAY repeat the request if it adds a valid Content-Length header field containing the length of the message-body in the request message.

So when you send an empty RequestBody in POST/PUT Method then you also need to send Content-Length:0. So add this header in your request. I don't think this header will cause a problem of you added into Request Object.

这篇关于获得“411长度要求”在来自HTTP客户端的PUT请求之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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