c# - 强制 HttpClient 获取新版本的源

标签 c# html .net http asynchronous

我目前正在做某事,它要求我在每次运行函数时都获取新版本的源代码。这是到目前为止的代码。

static class DataManager
{
    public static async void CollectData()
    {
        HttpClient client = new HttpClient();

        // Call asynchronous network methods in a try/catch block to handle exceptions 
        try
        {
            string responseBody = await client.GetStringAsync("http://www.lipsum.com");

            ParseHTML(responseBody);
        }
        catch (HttpRequestException e)
        {
            Console.WriteLine("Error: {0}", e.Message);
        }

        // Need to call dispose on the HttpClient object 
        // when done using it, so the app doesn't leak resources
        client.Dispose();
    }
    public static void ParseHTML(string _responseString)
    {
        HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();

        document.LoadHtml(_responseString);

        HtmlAgilityPack.HtmlNode contrib = document.DocumentNode.SelectSingleNode("//*[contains(@class,'randomclass')]");

        Console.WriteLine(contrib.InnerText.Replace(",", ""));
    }
    public static void UpdateLabels(string _timer, string _participants)
    {

    }
}

我想知道是否有办法让它在我每次运行该功能时都获得一个新版本的网站。

我正在通过输入运行它

DataManager.CollectData();

最佳答案

你试过吗:

var client = new HttpClient(new WebRequestHandler() {
    CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore)
});
        try
    {
        string responseBody = await client.GetStringAsync("http://www.lipsum.com");

        ParseHTML(responseBody);
    }
    catch (HttpRequestException e)
    {
        Console.WriteLine("Error: {0}", e.Message);
    }

看看 HttpRequestCacheLevel enum - 有很多选项可以帮助您。

关于c# - 强制 HttpClient 获取新版本的源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29401136/

相关文章:

c# - Linq 搜索原理和带有 OR 语句的子集合

html - 如何在我的案例中定位我的链接?

c# - DataContext - ListView - 刷新 UI - INotifyPropertyChanged

c# - 得到错误 : "Specified block size is not valid for this algorithm" while initialize AesCryptoProvider

c# - 通过统计分数实现更好的方法

c# - 如何在 xaml 中引用另一个模型?

c# - 根据数据库表列值禁用gridview行

c# - oauth/token 返回空体

html - 表格链接在 Firefox、IE8 中不起作用

html - Google Chrome Frame 的性能/技术问题? (适用于 IE 8 及更低版本)