HttpWebRequest
用于通过基本身份验证从 ASP.NET 站点下载文件。在许多情况下一切正常,但是如果带有状态代码的HttpWebRequest.GetResponse()
答案被分块,一些代理会将答案分块并引发异常。401
例外是:
System.Net.WebException: The server committed a protocol violation.
Section=ResponseStatusLine
at System.Net.HttpWebRequest.GetResponse()
Trace of answer is:<pre>
"HTTP/1.1 401 Authorization Required\r\nDate: Fri, 26 Jun 2009 04:45:18 GMT\r\nServer: Microsoft-IIS/6.0\r\nX-Powered-By: ASP.NET\r\nX-AspNet-Version: 2.0.50727\r\nWWW-Authenticate: Basic realm=\"iis-server\"\r\nCache-Control: private\r\nContent-Type: text/html; charset=iso-8859-1\r\nVia: 1.1 server\r\nKeep-Alive: timeout=15, max=100\r\nConnection: Keep-Alive\r\nTransfer-Encoding: chunked\r\nContent-Language: en\r\n\r\n0\r\n\r\n0\r\n\r\n
我做了测试,发现这Transfer-Encoding: chunked
是唯一的异常原因。这是 .NET Framework 2.0 的错误,还是有任何 RFC 说 401 答案不应该被分块?