由于某种原因,我理解对特定网站(https://learningnetwork.cisco.com/people/mrollins?view=profile)提出的请求会产生一个reqsponse对象,其响应流包含网站的主干版本。
流在65536字节之后结束,等于2 ^ 16字节。我认为这是一个可疑的数字。这些请求抱怨内部服务器错误,我对此表示赞同,因为我已经验证了webbrowsers能够理解这个响应,并且完整的html包含在服务器的响应中。 (使用fiddler)
我发现之前记录的问题here,由于它在本说明中结束的简单原因而不能令人满意:
“我想我不得不希望这个错误 不超过65536个字符......“
好吧。
变通方法很受欢迎,或者如果有人知道一个好的修复程序也很好。
using System;
using System.IO;
using System.Net;
using System.Web.UI;
namespace Example
{
public partial class _Default : Page
{
protected void Page_Load(object senderHidden, EventArgs eHidden)
{
//ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
var cookieContainer = new CookieContainer();
//Encoding enc = Encoding.UTF8;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://learningnetwork.cisco.com/people/mrollins?view=profile");
req.AllowAutoRedirect = false;
req.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
req.CookieContainer = cookieContainer;
req.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
req.Method = "GET";
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12";
req.KeepAlive = true;
HttpWebResponse resp = null;
try
{
resp = (HttpWebResponse)req.GetResponse();
}
catch (WebException e)
{
var r = e.Response as HttpWebResponse;
var memstream = Read(r.GetResponseStream());
var wrongLength = memstream.Length;
}
}
public static MemoryStream Read(Stream stream)
{
MemoryStream memStream = new MemoryStream();
byte[] readBuffer = new byte[4096];
int bytesRead;
while ((bytesRead = stream.Read(readBuffer, 0, readBuffer.Length)) > 0)
memStream.Write(readBuffer, 0, bytesRead);
return memStream;
}
}
}
答案 0 :(得分:16)
HttpWebRequest有一个静态属性,可以限制Web请求的长度。在请求发出之前插入的这行代码解决了这个问题。
HttpWebRequest.DefaultMaximumErrorResponseLength = 1048576;