WebException.Response.GetResponseStream()限制在65536个字符字符、Response、WebException、GetResponseStream

2023-09-04 11:59:56 作者:拨云见日

我试图使用从网页检索HTML code HttpWebRequest和HttpWebResponse。

I am trying to retrieve HTML code from a webpage using HttpWebRequest and HttpWebResponse.

response = (HttpWebResponse)request.GetResponse();
...
Stream stream = response.GetResponseStream();

相应对象的106142.一个CONTENTLENGTH值当我看到流对象,它有65536长度当读取使用ReadToEnd(),只有第65536字符返回一个StreamReader流。

The response object has a ContentLength value of 106142. When I look at the stream object, it has a length of 65536. When reading the stream with a StreamReader using ReadToEnd(), only the first 65536 characters are returned.

我怎样才能得到整个code?

How can I get the whole code?

编辑:

使用下面的code段:

Using the following code segment:

catch (WebException ex)
{
    errorMessage = errorMessage + ex.Message;
    if (ex.Response != null) {
        if (ex.Response.ContentLength > 0) 
        {
            using (Stream stream = ex.Response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    string pageOutput = reader.ReadToEnd().Trim();

ex.Response.ContentLength = 106142

ex.Response.GetResponseStream()。长度= 65536

ex.Response.GetResponseStream().Length = 65536

stream.Length = 65536

stream.Length = 65536

pageOutput.Length = 65534 (因为修剪)

pageOutput.Length = 65534 (because of the trim)

是的,在code实际上截断。

And yes, the code is actually truncated.

推荐答案

似乎有一个问题,呼吁由异常返回的HttpWebResponse的GetResponseStream()方法时。一切都按预期工作时也不例外。

There seems to be a problem when calling the GetResponseStream() method on the HttpWebResponse returned by the exception. Everything works as expected when there is no exception.

我希望得到从服务器返回的错误HTML code。

I wanted to get the HTML code from the error returned by the server.

我想我将不得不希望误差不超过65536字......

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

 
精彩推荐
图片推荐