gpt4 book ai didi

c# - 具有大数据集的不完整 HttpWebResponse

转载 作者:行者123 更新时间:2023-11-30 18:03:52 26 4
gpt4 key购买 nike

我有一些代码可以下载我已经使用了一段时间的网页的内容。这段代码工作正常,从来没有出现过问题,现在仍然没有……但是,有一个页面相当大(2MB,没有图像),有 4 个表,分别有 4、20、100、600 行和大约 20列宽。

当尝试获取所有数据时,它在没有任何明显错误或异常的情况下完成,但只返回第 4 个表中的大约第 60 行 - 有时更多,有时更少。浏览器在大约 20 到 30 秒内完成加载,并不断刷新页面,直到完成。

我已经尝试了 SO 的许多解决方案并进行了搜索,但没有任何不同的结果。下面是当前代码,但我有:代理、异步、无超时、假保活...

我不能使用 WebClient(作为另一个牵强附会的尝试),因为我需要使用 cookiecontainer 登录。

        HttpWebRequest pageImport = (HttpWebRequest)WebRequest.Create(importUri);
pageImport.ReadWriteTimeout = Int32.MaxValue;
pageImport.Timeout = Int32.MaxValue;
pageImport.UserAgent = "User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3";
pageImport.Accept = "Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
pageImport.KeepAlive = true;
pageImport.Timeout = Int32.MaxValue;
pageImport.ReadWriteTimeout = Int32.MaxValue;
pageImport.MaximumResponseHeadersLength = Int32.MaxValue;

if (null != LoginCookieContainer)
{
pageImport.CookieContainer = LoginCookieContainer;
}

Encoding encode = System.Text.Encoding.GetEncoding("utf-8");


using (WebResponse response = pageImport.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream, encode))
{
stream.Flush();
HtmlRetrieved = reader.ReadToEnd();
}

最佳答案

尝试以 block 方式读取而不是 reader.ReadToEnd();只是给你一个想法:

//将流通过管道传输到具有所需编码格式的更高级别的流读取器。 StreamReader readStream = new StreamReader( ReceiveStream, encode ); Console.WriteLine("\n收到响应流"); Char[] read = new Char[256];

    // Read 256 charcters at a time.    
int count = readStream.Read( read, 0, 256 );
Console.WriteLine("HTML...\r\n");

while (count > 0)
{
// Dump the 256 characters on a string and display the string onto the console.
String str = new String(read, 0, count);
Console.Write(str);
count = readStream.Read(read, 0, 256);
}

关于c# - 具有大数据集的不完整 HttpWebResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6739337/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com