php HTTP 错误 411,请求必须分块或具有内容长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43424677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:49:40  来源:igfitidea点击:

HTTP Error 411, The request must be chunked or have a content length

phpcurl

提问by erdeepak

I am trying to login to a remote website but getting error on below code "HTTP Error 411, The request must be chunked or have a content length."

我正在尝试登录远程网站,但在以下代码中出现错误“HTTP 错误 411,请求必须分块或具有内容长度”。

$username = "psker";
$password = "Admin123";
$url="https://192.18.11.33/Login.aspx?FromMasterLogin=true"; 
$postinfo = 'txtUserName='.$username.'&txtpassword='.$password.'&txtUserName_ClientState={"enabled":true,"emptyMessage":""}&txtpassword_ClientState={"enabled":true,"emptyMessage":""}&btnLogin_ClientState&btnClearSession_ClientState&rdwindowForget_ClientState&rdwindowEnforce_ClientState&rdWindowPublicNewsAlerts_ClientState&RadWindowManager1_ClientState';
$cookie_file_path = "/cookies.txt";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$headers = array( 
            "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
            "Accept-Encoding:gzip, deflate, br",
            "Accept-Language:en-US,en;q=0.8",
            "Cache-Control:max-age=0",
            "Connection:keep-alive",
            "Content-Length:1025",
            "Content-Type:application/x-www-form-urlencoded"
        );
curl_setopt($ch, CURLOPT_HTTPHEADER,  $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "https://192.18.11.33/RGCS/Default.aspx?dd=0");
$html = curl_exec($ch);
echo $html;
curl_close($ch);

Below are the original headers of login page :

以下是登录页面的原始标题:

Request URL:https://192.18.11.33/Login.aspx?FromMasterLogin=true
Request Method:POST
Status Code:302 Found
Remote Address:192.18.11.33:443
Referrer Policy:no-referrer-when-downgrade
Response Headers
view source
Cache-Control:private
Content-Length:34153
Content-Type:text/html; charset=utf-8
Date:Sat, 15 Apr 2017 09:37:35 GMT
Location:/RGCS/Default.aspx?dd=0
Server:Microsoft-IIS/8.5
Set-Cookie:ASP.NET_SessionId=spw3wky1bsdz0mrwzzojg504; path=/; HttpOnly
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
Request Headers
view source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:1025
Content-Type:application/x-www-form-urlencoded
Cookie:ASP.NET_SessionId=
DNT:1
Host:192.18.11.33
Origin:https://192.18.11.33
Referer:https://192.18.11.33/Login.aspx?FromMasterLogin=true
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
Query String Parameters
view source
view URL encoded
FromMasterLogin:true
Form Data
view source
view URL encoded
__EVENTTARGET:btnLogin
__EVENTARGUMENT:
__VIEWSTATE:/wEPDwULLTEzNDc1MTg5NDRkGAIFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYGBQhidG5Mb2dpbgUPYnRuQ2xlYXJTZXNzaW9uBRFSYWRXaW5kb3dNYW5hZ2VyMQUOcmR3aW5kb3dGb3JnZXQFD3Jkd2luZG93RW5mb3JjZQUYcmRXaW5kb3dQdWJsaWNOZXdzQWxlcnRzBQpyYWRDYXB0Y2hhDxQrAAIFJDcxZmM0ZThmLTRlYTktNDE2Mi1hZTM4LWE0ZmNkNzM0NzY3ZgYAAAAAAAAAAGTJGSQTauu1xAgiX10rd7/Zci9sJhXV9Ilqy4HDolIBqg==
__EVENTVALIDATION:/wEdAAci11URbCuVmlO2wf1gC0M7Y3plgk0YBAefRz3MyBlTcJxpWckI3qdmfEJVCu2f5cGinihG6d/Xh3PZm3b5AoMQf2Dr69OxAarGhVFbQWZWFpd+ecw9lQ5sg8SY03yGmgNKhPS/+yQ5+zLwEb8uDfAwho9uEQI2joMICVOBiz0yDgel4nUaIRbrrP5r1YBnzqE=
txtUserName:psibmaker
txtUserName_ClientState:{"enabled":true,"emptyMessage":""}
txtpassword:Admin!@123
txtpassword_ClientState:{"enabled":true,"emptyMessage":""}
btnLogin_ClientState:
btnClearSession_ClientState:
rdwindowForget_ClientState:
rdwindowEnforce_ClientState:
rdWindowPublicNewsAlerts_ClientState:
RadWindowManager1_ClientState:

So my problem is "The request must be chunked or have a content length."

所以我的问题是“请求必须分块或具有内容长度。”

Can anybody help me? Thanks for reading.

有谁能够帮助我?谢谢阅读。

回答by Marlon van der Linde

The server will not accept requests without a Content-Length header. I see you specified it in your headers as 1025. This length should be the number of bytes after the headers, the body itself. Is 1025 correct, as you have it hardcoded? The existence of a content-length header, indicates the presence of a message body (and the exact (octet) byte length of it, or the connection will close after this amount)

服务器不会接受没有 Content-Length 标头的请求。我看到您在标题中将其指定为 1025。此长度应该是标题后的字节数,即正文本身。1025 是否正确,因为您对其进行了硬编码?content-length 标头的存在,表明消息体的存在(以及它的确切(八位字节)字节长度,否则连接将在此数量后关闭)

According to https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

根据https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

"The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of the entity-body that would have been sent had the request been a GET"

“内容长度实体头字段指示实体主体的大小,以八进制十进制数表示,发送给接收者,或者在 HEAD 方法的情况下,实体主体的大小将被发送如果请求是 GET”

I also notice you are using ""Content-Type:application/x-www-form-urlencoded"" in which case consider that the content length must be the length of the url encoded form/whatever data.

我还注意到您正在使用 ""Content-Type:application/x-www-form-urlencoded"" 在这种情况下,请考虑内容长度必须是 url 编码表单/任何数据的长度。

I hope this helps!

我希望这有帮助!