重定向期间在RestSharp中保留授权标头

编程入门 行业动态 更新时间:2024-10-05 15:24:32
本文介绍了重定向期间在RestSharp中保留授权标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用RestSharp进行GET api调用.通过传递授权标头,通过HTTP Basic身份验证对api调用进行身份验证.

I am using RestSharp to make a GET api call. The api call is authenticated through HTTP Basic authentication by passing the authorization header.

服务器使用状态码307重定向api调用.我的客户端代码确实处理了重定向,但是未将授权标头传递给此重定向的api调用.出于此处所述的正当理由而完成此操作.因此,我确实收到了未经授权的错误.

The server redirects the api call with a status code 307. My client code does handle the redirects but the authorization header is not passed to this redirected api call. This is done for valid reasons as mentioned here. Hence I do get an unauthorized error.

如何配置RestClient还原授权标头?

How can I configure the RestClient to restore the authorization header?

var client = new RestClient("serverurl"); var request = new RestRequest(Method.GET); request.AddHeader("Authorization", "Basic Z3JvdXAxOlByb2otMzI1"); request.AddHeader("Content-Type", "application/json"); request.AddHeader("Tenant-Id", "4892"); IRestResponse response = client.Execute(request); Console.WriteLine(response.Content);

推荐答案

我添加了一条检查,该检查重新发送了使用以下代码接收401的api请求.

I added a check that resends the api request of receiving a 401 with the below code.

var client = new RestClient("serverurl"); var request = new RestRequest(Method.GET); request.AddHeader("Authorization", "Basic Z3JvdXAxOlByb2otMzI1"); request.AddHeader("Content-Type", "application/json"); request.AddHeader("Tenant-Id", "4892"); IRestResponse response = client.Execute(request); //Resend the request if we get 401 int numericStatusCode = (int)response.StatusCode; if(numericStatusCode == 401) { var redirectedClient = new RestClient(response.ResponseUri.ToString()); IRestResponse newResponse = redirectedClient.Execute(request); Console.WriteLine(newResponse.ResponseStatus); }

更多推荐

重定向期间在RestSharp中保留授权标头

本文发布于:2023-11-03 04:45:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554269.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   RestSharp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!