HttpWebRequest的VS Web客户端(特殊情况) [英] HttpWebRequest vs Webclient (Special scenario)

查看:118
本文介绍了HttpWebRequest的VS Web客户端(特殊情况)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经在这线程回答过,但我不能似乎找到细节。

I know this question has been answered before in this thread, but I couldn't seem to find the details.

在我的方案,我建立一个控制台应用程序,将密切关注任何更改HTML页面源代码。如果发生任何更新/改变,我将执行进一步的操作。此外,我还会每隔1秒之后执行的请求,或尽快以前的要求完成。

In my scenario, I am building a console application which will keep an eye on html page source for any changes. If any update/change occurs, I will perform further operations. Moreover, I'll also perform a request after every 1 second, or as soon as the previous request finishes.

我似乎无法弄清楚,我应该使用的HttpWebRequest Web客户端下载html页面源代码并执行比较呢?你认为什么将是我的情况下,理想的解决方案?速度和可靠性都:)

I can't seem to figure out should I use HttpWebRequest or WebClient for downloading the html page source and perform comparison? What do you think would be an ideal solution in my case? Speed and reliability both :)

推荐答案

我会去与 HttpWebRequst 因为它不是抽象的,让你用HTTP PARAMS捣鼓了不少。它给你如果服务器返回文件没有改变,例如不下载整个页面的选项。

I'd go with HttpWebRequst because it's not as abstracted and lets you fiddle with HTTP params quite a bit. It gives you the option to not download the entire page if the server returns "file not changed", for example.

如果您的一些参数添加到您的要求像<一个HREF =http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.ifmodifiedsince.aspx> IfModifiedSince (它可能是头部或GET请求)的服务器可能会返回响应代码的 304 - 不修改即可。参阅作进一步的解释缓存href=\"http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html\">描述。

If you add some parameters to your request like IfModifiedSince (it might be HEAD or GET request) the server may return the response code 304 - NOT MODIFIED. Refer to description of caching in HTTP for further explanation.

问题的关键是要确保你只下载完整的页面时,它实际上是因为你获取它的最后一次修改。大部分资金将不会被改变的时间(我想,也不能确切知道不知道您的域名),所以你只需要从服务器获取一个轻量级的响应它简单地指出,这里没有什么改变。

The point is to make sure that you only download the full page when it's actually modified since the last time you fetched it. Most of the time it won't be changed (I suppose, can't know for sure without knowing your domain), so you only need to get a lightweight response from server which simply states "nothing changed here".

更新:代码示例展示了如何使用 IfModifiedSince 属性:

Update: code sample demonstrating the use of IfModifiedSince property:

bool IsResourceModified(string url, DateTime dateTime) {            
    try {
        var request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
        request.IfModifiedSince = dateTime;
        request.Method = "HEAD";
        var response = (HttpWebResponse)request.GetResponse();

        return true;
    }
    catch(WebException ex) {
        if(ex.Status != WebExceptionStatus.ProtocolError)
            throw;

        var response = (HttpWebResponse)ex.Response;
        if(response.StatusCode != HttpStatusCode.NotModified)
            throw;

        return false;    
    }
}

这方法应该返回如果页面是因为日期时间日期体改和,如果事实并非如此。 的GetResponse 方法将抛出 引发WebException 如果你犯了一个HEAD请求,服务器返回 304 - 不修改(这是有点不幸的)。我们必须确保它不是一些其他网站的连接问题,这就是为什么我检查网络异常的状态和响应HTTP状态。如果别的导致异常,我们只是把它进一步。

This method should return true if the page was modifed since the dateTime date and false if it wasn't. GetResponse method will throw a WebException if you make a HEAD-request and the server returns 304 - NOT MODIFIED (which is kinda unfortunate). We have to make sure that it's not some other web connection problem, that's why I check the status of web exception and the HTTP status in response. If anything else caused an exception we just throw it further.

Console.WriteLine(IsResourceModified("http://example.com", new DateTime(2009)));
Console.WriteLine(IsResourceModified("http://example.com", DateTime.Now));

此示例代码生成的输出:

This sample code produces the output:

True
False

注意: 确保他给出了这一技术少有的好建议阅读吉姆米契尔的除了这个答案

Note: make sure to read Jim Mischel's addition to this answer as he gives few good advices on this technique.

这篇关于HttpWebRequest的VS Web客户端(特殊情况)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆