[英]C# how to retry if await url.GetStringAsync null?
I want to ask about how to retry if await url.GetStringAsync
get cancel task? 我想问一下如何重试是否
await url.GetStringAsync
获取取消任务?
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var httpclient = new HttpClient();
var html = await httpclient.GetStringAsync(url);
if you wish to do so in the code above (rather than using the HttpResponse
object) then you could do something like below 如果您希望在上面的代码中这样做(而不是使用
HttpResponse
对象),则可以执行以下操作
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var httpclient = new HttpClient();
int retries = 5;
int count = 0;
var html = await httpclient.GetStringAsync(url);
while(String.IsNullOrEmpty(html) && count++ < retries) {
var html = await httpclient.GetStringAsync(url); }
if(String.IsNullOrEmpty(html)) {
throw new Exception("Could not contact the server"); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.