gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:07:25 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com