目前,我正在向我的请求添加一个授权标头,如下所示:
文件:SomeFile.cs
public interface ITestApi
{
[Get("/api/test/{id}")]
Task<string> GetTest([Header("Authorization")] string authorization, int id);
[Get("/api/tests/")]
Task<string> GetTests([Header("Authorization")] string authorization);
}
文件:SomeOtherFile.cs
var username = "xxx";
var password = "yyy";
var authHeader = "Basic " + Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
var baseAddress = "https://my.test.net";
ITestApi myApi = RestService.For<ITestApi>(baseAddress);
int id = 123;
var test= myApi.GetTest(authHeader, id);
var tests = myApi.GetTests(authHeader);
有没有办法全局设置 authHeader,这样我就不必将它作为参数传递给每个调用?(我使用的是改装版本 4.6.48)。换句话说,我希望能够进行这样的调用:
var test= myApi.GetTest(id);
var tests = myApi.GetTests();