Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用“重试后”标头使用asp.net http客户端轮询API_C#_.net_Http_Httpclient - Fatal编程技术网

C# 如何使用“重试后”标头使用asp.net http客户端轮询API

C# 如何使用“重试后”标头使用asp.net http客户端轮询API,c#,.net,http,httpclient,C#,.net,Http,Httpclient,我对在.net中使用http客户机进行RESTful消费有点陌生,在轮询外部API时,我很难理解如何使用retry after标头 这是我目前必须进行的投票: HttpResponseMessage result = null; var success = false; var maxAttempts = 7; var attempts = 0; using (var client = new HttpClient()) { do { var url = &q

我对在.net中使用http客户机进行RESTful消费有点陌生,在轮询外部API时,我很难理解如何使用retry after标头

这是我目前必须进行的投票:

HttpResponseMessage result = null;
var success = false;
var maxAttempts = 7;
var attempts = 0;

using (var client = new HttpClient()) 
{
    do
    {
        var url = "https://xxxxxxxxxxxxxxx";
        result = await client.GetAsync(url);

        attempts++;

        if(result.StatusCode == HttpStatusCode.OK || attempts == maxAttempts) 
           success = true;
    }
    while (!success);
}

return result;
如您所见,我一直轮询端点,直到得到OK响应或达到最大尝试次数(停止连续循环)

如何使用得到的响应中的retry after标头来指示循环中每个调用之间的等待时间

我就是想不出如何把这个应用到我的情况中去

谢谢,

旨在为每个应用程序而不是每次使用实例化一次

private static HttpClient=new HttpClient();
方法(使用HTTP主机头更新)

private静态异步任务GetDataWithPollingAsync(字符串url,int-maxAttempts,字符串主机=null)
{
使用(HttpRequestMessage请求=新的HttpRequestMessage(HttpMethod.Get,url))
{
如果(host?.Length>0)request.Headers.host=host;
for(int-trunt=0;trunt
用法

试试看
{
字符串结果=等待GetDataWithPollingAsync(“http://some.url,7,“www.example.com”);
//收到
}
捕获(例外情况除外)
{
Debug.WriteLine(例如消息);
//失败
}

使用此代码添加标题的有效方法是什么?我有不同的头为每一种情况下使用这个method@DBoi例如,哪些标题?比如主机标题或内容类型标题?我需要将它们作为单独的参数传入,还是可以将它们作为一组标题传入?好的,我来检查一下-非常感谢您花时间提供帮助。