C# 具有大数据集的不完整HttpWebResponse
我有一些代码可以下载我已经使用了一段时间的网页内容。这段代码运行良好,从来没有提供任何问题,仍然没有。。。然而,有一个页面相当大(2MB,没有图像),有4个表,分别有4、20、100、600行和大约20列宽 当尝试获取所有数据时,它会在没有任何明显错误或异常的情况下完成,但只返回第4个表中的大约第60行—有时更多,有时更少。broswer在大约20-30秒内完成加载,并不断刷新页面,直到加载完成 我尝试了SO和搜索中的许多解决方案,但没有得到任何不同的结果。下面是当前代码,但我有:代理、异步、无超时、FalseKeepAlive 我无法使用WebClient(作为另一个far fetch尝试),因为我需要使用cookiecontainer登录C# 具有大数据集的不完整HttpWebResponse,c#,.net,httpwebrequest,screen-scraping,httpwebresponse,C#,.net,Httpwebrequest,Screen Scraping,Httpwebresponse,我有一些代码可以下载我已经使用了一段时间的网页内容。这段代码运行良好,从来没有提供任何问题,仍然没有。。。然而,有一个页面相当大(2MB,没有图像),有4个表,分别有4、20、100、600行和大约20列宽 当尝试获取所有数据时,它会在没有任何明显错误或异常的情况下完成,但只返回第4个表中的大约第60行—有时更多,有时更少。broswer在大约20-30秒内完成加载,并不断刷新页面,直到加载完成 我尝试了SO和搜索中的许多解决方案,但没有得到任何不同的结果。下面是当前代码,但我有:代理、异步、无
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();
}
尝试按块读取而不是读卡器。ReadToEnd(); 给你一个想法: //使用所需的编码格式将流传送到更高级别的流读取器。 StreamReader readStream=新的StreamReader(接收流,编码); Console.WriteLine(“\n收到响应流”); Char[]read=新字符[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);
}
我怀疑这是作为服务器端的配置设置处理的。顺便说一句,我认为您可能设置了错误的属性。从文本中删除“用户代理”和“接受”,如下所示:
pageImport.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3";
pageImport.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
虽然我仍将尝试提供的建议,并将改变我的答案,如果它起作用,似乎在这种情况下,问题是代理。我站在代理前面,代码按预期工作,速度更快 我必须看一些代理优化,因为这段代码必须在代理后面运行