问题描述
我请求所有404我写了100页
I requested 100 pages that all 404. I wrote
{
var s = DateTime.Now;
for(int i=0; i < 100;i++)
DL.CheckExist("http://google.com/lol" + i.ToString() + ".jpg");
var e = DateTime.Now;
var d = e-s;
d=d;
Console.WriteLine(d);
}
static public bool CheckExist(string url)
{
HttpWebRequest wreq = null;
HttpWebResponse wresp = null;
bool ret = false;
try
{
wreq = (HttpWebRequest)WebRequest.Create(url);
wreq.KeepAlive = true;
wreq.Method = "HEAD";
wresp = (HttpWebResponse)wreq.GetResponse();
ret = true;
}
catch (System.Net.WebException)
{
}
finally
{
if (wresp != null)
wresp.Close();
}
return ret;
}
两个运行表明它采取00:00:30.7968750 00:00:26.8750000。然后,我尝试Firefox和使用以下code
Two runs show it takes 00:00:30.7968750 and 00:00:26.8750000. Then i tried firefox and use the following code
<html>
<body>
<script type="text/javascript">
for(var i=0; i<100; i++)
document.write("<img src=http://google.com/lol" + i + ".jpg><br>");
</script>
</body>
</html>
使用我的补偿时间和计数是大约4秒。 4秒是6.5-7.5faster然后我的应用程序。我打算通过成千上万的文件扫描因此服用3.75小时,而不是30分钟将是一个大问题。我怎样才能让这个code更快?我知道有人会说,Firefox的缓存图像,但我想说1)它仍然需要从远程服务器检查头,看它是否已被更新(这是我想我的应用程序做的)2)我不是接收体,我的code只应请求报头。所以,我怎么解决这个问题?
Using my comp time and counting it was roughly 4 seconds. 4 seconds is 6.5-7.5faster then my app. I plan to scan through a thousands of files so taking 3.75hours instead of 30mins would be a big problem. How can i make this code faster? I know someone will say firefox caches the images but i want to say 1) it still needs to check the headers from the remote server to see if it has been updated (which is what i want my app to do) 2) I am not receiving the body, my code should only be requesting the header. So, how do i solve this?
推荐答案
火狐大概一次,而你的code发出多个请求做他们一个接一个。也许添加线程会加快你的程序。
Probably Firefox issues multiple requests at once whereas your code does them one by one. Perhaps adding threads will speed up your program.
这篇关于这是为什么的WebRequest code慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!