HTTP Error 411: Why Your POST Request Fails Without Content-Length
When you encounter HTTP Error 411, the server is explicitly rejecting your request because it lacks sufficient information to determine how much data is coming in. The error message "The request must be chunked or have a content length" indicates that for a POST request, the HTTP/1.1 specification requires either a Content-Length header specifying the exact byte count of the payload or the use of chunked transfer encoding.
In the scenario you described, your PHP script attempts to automate a login sequence using cURL. While the code sets CURLOPT_POST and populates CURLOPT_POSTFIELDS, it fails to correctly calculate or set the Content-Length header in the custom headers array. This mismatch triggers the 411 response immediately upon sending the request.
Calculating the Payload Size
The root cause lies in how cURL handles headers versus how you are manually constructing them. When you pass data via CURLOPT_POSTFIELDS, cURL automatically calculates the length of that string and includes a Content-Length header in the default request stream. However, your code overrides this behavior by defining a custom $headers array that explicitly lists "Content-Length:1025".
If the actual byte count of your $postinfo string differs from 1025, or if cURL's internal logic conflicts with your manual header injection, the server receives an inconsistent request. Modern IIS servers (like the Microsoft-IIS/8.5 indicated in your response headers) are strict about this requirement. They expect the Content-Length to match the body exactly. If you manually define a header that doesn't match reality, or if you fail to let cURL manage it entirely, the server rejects the packet before processing the form data.
Fixing the cURL Configuration
To resolve this, you must stop manually defining the Content-Length header in your $headers array when using CURLOPT_POSTFIELDS. Let cURL handle the calculation automatically. Additionally, ensure that the payload does not contain raw HTML entities like & unless they are part of a URL-encoded string intended for the server.
In your specific case, the variable $postinfo contains strings like txtUserName=&txtpassword=. In PHP, this renders as & in the output, but if you copy-pasted from a source where HTML entities were not decoded, the actual string length sent over the wire might differ from what you expect.
Here is the corrected approach. Remove the manual Content-Length entry and allow cURL to generate it:
$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"
// Remove "Content-Length:1025" from here
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// ... other options ...
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
By removing the manual header definition, cURL will inspect $postinfo, calculate its true byte size, and append the correct Content-Length header automatically. This aligns with the HTTP/1.1 standard and satisfies the IIS server's requirement.
Handling the ViewState and Form Data
Beyond the 411 error, there is a secondary issue regarding the form data integrity. In your original code, $postinfo includes fields like __VIEWSTATE and __EVENTVALIDATION. These are hidden tokens required by ASP.NET applications to verify the request's origin and prevent cross-site request forgery (CSRF).
Your code attempts to send these tokens as empty strings:
$__EVENTTARGET:btnLogin
__EVENTARGUMENT:
__VIEWSTATE:/wEPDwULLTEzNDc1MTg5NDRkGAIFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYGBQhidG5Mb2dpbgUPYnRuQ2xlYXJTZXNzaW9uBRFSYWRXaW5kb3dNYW5hZ2VyMQUOcmR3aW5kb3dGb3JnZXQFD3Jkd2luZG93RW5mb3JjZQUYcmRXaW5kb3dQdWJsaWNOZXdzQWxlcnRzBQpyYWRDYXB0Y2hhDxQrAAIFJDcxZmM0ZThmLTRlYTktNDE2Mi1hZTM4LWE0ZmNkNzM0NzY3ZgYAAAAAAAAAAGTJGSQTauu1xAgiX10rd7/Zci9sJhXV9Ilqy4HDolIBqg==
__EVENTVALIDATION:/wEdAAci11URbCuVmlO2wf1gC0M7Y3plgk0YBAefRz3MyBlTcJxpWckI3qdmfEJVCu2f5cGinihG6d/Xh3PZm3b5AoMQf2Dr69OxAarGhVFbQWZWFpd+ecw9lQ5sg8SY03yGmgNKhPS/+yQ5+zLwEb8uDfAwho9uEQI2joMICVOBiz0yDgel4nUaIRbrrP5r1YBnzqE=
While the code snippet provided seems to include these values, a common pitfall in automation scripts is sending an empty ViewState or failing to update it after the initial GET request. If your script does not perform a HEAD or GET request to /Login.aspx first to capture the fresh ViewState token before sending the POST, the server will reject the login attempt with a 400 Bad Request or 411-like errors because the anti-forgery tokens are invalid.
Ensure your script follows this flow:
- Initialize cURL.
- Perform a
GET(orHEAD) request to the login page to populate the cookies and fetch the ViewState. - Extract the ViewState from the response body.
- Construct the POST payload with the valid ViewState.
- Execute the
POST.
Conclusion
The HTTP 411 error is a strict adherence to protocol requirements. By removing the manually defined Content-Length header and allowing cURL to compute the payload size, you align your request with the server's expectations. Furthermore, ensure that any ViewState tokens required by ASP.NET are freshly retrieved before submission, as sending stale or empty tokens will ultimately fail authentication regardless of the content length.
