在 HttpClient 的多个重定向中传递授权 Header

Passing Authorization Header in multiple redirects of HttpClient

我正在向服务器发送 get 请求,服务器正在重定向到 4 furthur 调用,然后 returns 响应。 HttpClient 给出异常,未提供授权令牌。

你能告诉我,如何在多个重定向中传递相同的授权令牌吗?如果我设置 "AllowAutoRedirect=false" & returns RanToComplation Status,它工作正常。 这是我的方法:

static async Task CallWebAPIAsync()
        {

            WebRequestHandler webRequestHandler = new WebRequestHandler();
            webRequestHandler.UseDefaultCredentials = true;
             webRequestHandler.AllowPipelining = true;
            webRequestHandler.AllowAutoRedirect = true;

            using (var client = new HttpClient(webRequestHandler))
            {
                client.BaseAddress = new Uri("https://apirequest");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", token);
                client.Timeout = TimeSpan.FromSeconds(30000);
                //GET Method  
                var response = await client.GetAsync("user").ConfigureAwait(false); 
                if (response.IsSuccessStatusCode)
                {
                    var responseresult = await response.Content.ReadAsStringAsync();
                    Console.WriteLine("Id:{0}\tName:{1}", responseresult);
                }
                else
                {
                    Console.WriteLine("Internal server Error");
                }

            }

我缺少委托处理程序,它将处理重定向。 这 link 对我有帮助

更新后的代码为:

 static async Task CallWebAPIAsync()
    {
        HttpClientHandler clientHandler = new HttpClientHandler();
        WebRequestHandler webRequestHandler = new WebRequestHandler();
        webRequestHandler.UseDefaultCredentials = true;
         webRequestHandler.AllowPipelining = true;
        webRequestHandler.AllowAutoRedirect = false;
        webRequestHandler.Credentials = CredentialCache.DefaultCredentials;


        GlobalRedirectHandler globalRedirectHandler = new GlobalRedirectHandler() { InnerHandler = webRequestHandler };


        using (var client = new HttpClient(globalRedirectHandler))
        {
            client.BaseAddress = new Uri("https://apitest");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", token);


            client.Timeout = TimeSpan.FromSeconds(30000);
            //GET Method  
            var response = await client.GetAsync("user").ConfigureAwait(false); 
            if (response.IsSuccessStatusCode)
            {
                var responseresult = await response.Content.ReadAsStringAsync();
                Console.WriteLine("Id:{0}\tName:{1}", responseresult);
            }
            else
            {
                Console.WriteLine("Internal server Error");
            }

        }
    }

我认为问题可能与this

有关

必须在重定向时取消授权 headers。我目前正在想办法解决这个问题。