System.Net.HttpWebResponse.GetResponseStream()返回截断体WebException

编程入门 行业动态 更新时间:2024-10-26 00:32:06
本文介绍了System.Net.HttpWebResponse.GetResponseStream()返回截断体WebException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

出于某种原因,我不明白请求到perticular网站制作(learningnetwork.cisco/people/mrollins?view=profile)导致reqsponse对象,其responsestream包含一个网站的trunkated版本。

流后65536字节,它等于2 ^ 16字节结束。我认为这是一个可疑的轮数。该请求抱怨内部​​服务器错误,我河畔preSS的,因为我已经验证了这两者都化网页浏览器能够做出这种反应的意识,并充分HTML是包括在从服务器的响应。 (使用小提琴手)

我已经找到了问题previously记录here,这是不能令人满意的,它在这个音符结束,原因很简单:

  

我想我会希望错误   不超过65536字......

那么它的作用。

解决方法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(learningnetwork.cisco/people/mrollins?view=profile);             req.AllowAutoRedirect = FALSE;             req.AutomaticDecom pression = DECOM pressionMethods.Deflate | DECOM pressionMethods.GZip;             req.Co​​okieContainer =的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 (learningnetwork.cisco/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:

"I guess I'll have to hope the error doesn't exceed 65536 characters..."

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("learningnetwork.cisco/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

本文发布于:2023-11-10 03:06:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574179.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Net   System   HttpWebResponse   WebException   GetResponseStream

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!