强制HttpClient获取新版本的源代码

时间:2015-04-01 21:25:42

标签: 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();

1 个答案:

答案 0 :(得分:1)

你试过了吗?

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枚举 - 有很多选项可以帮助您。