【发布时间】:2012-11-28 18:45:27
【问题描述】:
我获取网页是为了向我的应用程序提供数据。但是,这些页面包含很多我根本不需要的图像。我只需要文本数据。 我的问题是网络请求花费了不可接受的时间。我认为图像也是在网络请求期间获取的。有没有办法消除图像并只下载文本数据?
以下是我目前正在使用的代码。
var httpWebRequest = HttpWebRequest.Create(url) as HttpWebRequest;
httpWebRequest.Method = "GET";
httpWebRequest.ProtocolVersion = HttpVersion.Version11;
httpWebRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
httpWebRequest.Proxy = null;
httpWebRequest.KeepAlive = true;
httpWebRequest.Accept = "text/html";
string responseString = null;
var httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
using (var responseStream = httpWebResponse.GetResponseStream())
{
using (var streamReader = new StreamReader(responseStream))
{
responseString = streamReader.ReadToEnd();
}
}
此外,欢迎提出任何其他优化建议。
【问题讨论】:
-
Is there any way to eliminate the images and download only the text data?你已经只下载了纯 html 而不是图片。 -
@L.B - 好的。那么是不是没有进一步优化的空间了?
-
优化速度,代码?。例如,如果您使用
WebClient,您可以用两行编写代码。 (当然,WebClient 在功能方面是 HttpWebRequest 的子集) -
@L.B - 哦,是的!一个小时前我开始了解 WebClient 并阅读了它。我将用 WebClient 替换现有代码,因为我似乎没有使用 HttpWebRequest 提供的额外控制功能。我要问的是速度优化。
标签: c# .net http-headers httpwebrequest