我写了一个类,可以下载网站的app.use(cors());
app.options('*', cors());
结构。这很好用,但是有时(我无法弄清楚为什么)方法html
返回一个空字符串,这给我带来了很多问题,因为返回的“ html”里面没有文本,然后使用ReadAsStringAsync()
的方法将无法对其进行分析。这是我的课程:
HtmlAgilityPack
该类还处理重定向,但是我想将注意力集中在public class NetworkHelper
{
/// <summary>
/// Configure the client to handle web request.
/// </summary>
static Lazy<HttpClient> httpClient = new Lazy<HttpClient>(() =>
{
var handler = CreateHandler();
return new HttpClient(handler)
{
Timeout = TimeSpan.FromSeconds(60)
};
});
/// <summary>
/// Web request handler.
/// </summary>
/// <returns>Return an object HttpMessageHandler.</returns>
static HttpMessageHandler CreateHandler()
{
var handler = new HttpClientHandler();
//If the framework support it, se a specific redirect.
if (handler.SupportsRedirectConfiguration)
{
handler.AllowAutoRedirect = true;
handler.MaxAutomaticRedirections = 5;
}
//Set page compression.
if (handler.SupportsAutomaticDecompression)
{
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
}
return handler;
}
/// <summary>
/// Get the html from a web page.
/// </summary>
/// <param name="url">Url of the site.</param>
/// <returns>Return a string that contains the html.</returns>
public async Task<string> GetHtmlAsync(Uri url, CancellationToken cancellationToken = default(CancellationToken))
{
//This protocol is used to exchange the information from client to server.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
//Get web page html.
var response = await httpClient.Value.GetAsync(url, cancellationToken);
var content = await response.Content.ReadAsStringAsync();
return content;
}
}
上,该方法效果很好,但通常返回一个GetHtmlAsync
字符串。我以为这可能是服务器繁忙或类似的情况,在这种情况下,我该如何处理这种情况?
感谢任何提示。
答案 0 :(得分:0)
有时服务器正忙,并且不发送任何请求,目前我正在使用以下代码修复此问题:
if (!response.IsSuccessStatusCode)
content = await new NetworkHelper().GetHtmlAsync(url);
else
content = await response.Content.ReadAsStringAsync()
return content;
本质上,如果状态代码不成功,则该方法将再次执行,这对我有用,也许有人可以建议其他解决方案