这就是我正在尝试的:
public static async Task<HttpResponseMessage> post(string specificUrl, string token, StringContent inputData) // specificUrl like "user/" at the end "/"
{
var policy = Policy
.Handle<Exception>()
.OrResult<HttpResponseMessage>(res => !res.IsSuccessStatusCode)
.RetryAsync(3);
string fullUrl = baseUrl + specificUrl;
using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, fullUrl))
{
requestMessage.Content = inputData;
requestMessage.Headers.Add("access_token", token);
//HttpResponseMessage res = await client.SendAsync(requestMessage).ConfigureAwait(false);
HttpResponseMessage res = await policy.ExecuteAsync(() => client.SendAsync(requestMessage)).ConfigureAwait(false);
return res;
}
}
Run Code Online (Sandbox Code Playgroud)
一旦代码到达 waitAndRetryPolicy 并等待所需的时间,我就会收到以下错误:
System.InvalidOperationException:'请求消息已发送。无法多次发送相同的请求消息。
c# ×1