System.Net.HttpWebResponse.GetResponseStream() 在 WebException 中返回截断的正文

发布于 2024-10-16 13:51:46 字数 2694 浏览 5 评论 0原文

由于某种超出我理解的原因,向特定网站提出请求 (https://learningnetwork.cisco .com/people/mrollins?view=profile)会生成一个 reqsponse 对象,其响应流包含网站的中继版本。

该流在 65536 字节后结束,相当于 2^16 字节。我认为这是一个可疑的整数。这些请求抱怨内部服务器错误,我对此表示抑制,因为我已经验证了网络浏览器能够理解此响应,并且完整的 html 包含在服务器的响应中。 (使用fiddler

我发现了之前记录的问题此处,这并不令人满意,原因很简单,它在此注释上结束:

“我想我必须希望这个错误 不超过 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;
        }
    }
}

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:

"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("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;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅沫记忆 2024-10-23 13:51:46

HttpWebRequest 有一个静态属性,可以限制 Web 请求的长度。在发出请求之前插入这行代码可以解决问题。

HttpWebRequest.DefaultMaximumErrorResponseLength = 1048576;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文