问题描述
出于某种原因,我不明白请求到perticular网站制作(https://learningnetwork.cisco.com/people/mrollins?view=profile)导致reqsponse对象,其responsestream包含一个网站的trunkated版本。
流后65536字节,它等于2 ^ 16字节结束。我认为这是一个可疑的轮数。该请求抱怨内部服务器错误,我河畔preSS的,因为我已经验证了这两者都化网页浏览器能够做出这种反应的意识,并充分HTML是包括在从服务器的响应。 (使用小提琴手)
我已经找到了问题previously记录here,这是不能令人满意的,它在这个音符结束,原因很简单:
那么它的作用。
解决方法AP preciated,或者如果有谁知道一个upcomming修补程序是不错。
使用系统;
使用System.IO;
使用System.Net;
使用System.Web.UI程序;
命名空间示例
{
公共部分类_Default:页
{
保护无效的Page_Load(对象senderHidden,EventArgs的eHidden)
{
//ServicePointManager.ServerCertificateValidationCallback + =委托(对象发件人,X509证书证书,X509Chain链,SslPolicyErrors sslPolicyErrors){返回true; };
VAR的CookieContainer =新的CookieContainer();
//编码ENC = Encoding.UTF8;
HttpWebRequest的REQ =(HttpWebRequest的)WebRequest.Create(https://learningnetwork.cisco.com/people/mrollins?view=profile);
req.AllowAutoRedirect = FALSE;
req.AutomaticDecom pression = DECOM pressionMethods.Deflate | DECOM pressionMethods.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;如; RV:1.8.1.12)的Gecko / 20080201火狐/ 2.0.0.12;
req.KeepAlive = TRUE;
HttpWebResponse RESP = NULL;
尝试
{
RESP =(HttpWebResponse)req.GetResponse();
}
赶上(WebException E)
{
变种R = e.Response为HttpWebResponse;
VAR memstream =读(r.GetResponseStream());
VAR wrongLength = memstream.Length;
}
}
公共静态的MemoryStream读(流流)
{
MemoryStream的memStream =新的MemoryStream();
byte []的readBuffer =新的字节[4096];
INT读取动作;
而((读取动作= stream.Read(readBuffer,0,readBuffer.Length))大于0)
memStream.Write(readBuffer,0,读取动作);
返回memStream;
}
}
}
HttpWebRequest的有一个静态属性,限制Web请求的长度。这条线的code,插在提出要求之前,解决了这个问题。
HttpWebRequest.DefaultMaximumErrorResponseLength = 1048576;
For some reason beyond my understanding requests made to a perticular website (https://learningnetwork.cisco.com/people/mrollins?view=profile) result in a reqsponse-object whose responsestream contain a trunkated version of the website.
The stream ends after 65536 bytes, which equals 2^16 bytes. I consider that a suspiciously round number. The requests complains of an internal server error, which I surpress, because I have already verified both that webbrowsers are able to make sense of this response, and that the full html is included in the response from the server. (using fiddler)
I've found the problem previously documented here, which is unsatisfactory for the simple reason that it ends on this note:
Well it does.
Workarounds appreciated, or if anyone knows of an upcomming fix that is good too.
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;
}
}
}
HttpWebRequest has a static property that limits the length of web requests. this line of code, inserted before the request is made, solves the problem.
HttpWebRequest.DefaultMaximumErrorResponseLength = 1048576;
这篇关于System.Net.HttpWebResponse.GetResponseStream()返回截断体WebException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!