2017-04-15 88 views
2

我正在嘗試登錄到遠程網站,但在以下代碼中出現錯誤「HTTP錯誤411,請求必須分塊或具有內容長度。」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); 

下面是登錄頁面的原標題:「請求必須進行分塊或具有內容長度」

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:[email protected] 
txtpassword_ClientState:{"enabled":true,"emptyMessage":""} 
btnLogin_ClientState: 
btnClearSession_ClientState: 
rdwindowForget_ClientState: 
rdwindowEnforce_ClientState: 
rdWindowPublicNewsAlerts_ClientState: 
RadWindowManager1_ClientState: 

所以我的問題

任何人都可以幫助我嗎?謝謝閱讀。

回答

2

服務器不接受沒有Content-Length標頭的請求。我看到你在你的頭文件中將它指定爲1025.這個長度應該是頭部之後的字節數,即身體本身。 1025是否正確,因爲你有硬編碼?一個內容長度報頭的存在,指示消息體的存在(以及它的確切(八位位組)字節的長度,或該量之後的連接將關閉)

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

「Content-Length實體標題字段指示向收件人發送的十進制數的OCTET中的實體主體的大小,或者在HEAD方法的情況下,實體主體的大小爲 已發送請求已獲得GET「

我還注意到你正在使用「」Content-Type:application/x-www-form-urlencoded「」,在這種情況下,考慮內容長度必須是URL編碼格式/任何數據的長度。

我希望這有助於!

相關問題