Getting “411 Length Required” after a PUT request from HTTP Client

三世轮回 提交于 2019-12-01 14:35:44

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/15619562/getting-411-length-required-after-a-put-request-from-http-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!