如何在xamarin.forms中添加带有修改的静态授权标头?

时间:2019-07-03 10:25:49

标签: c# xamarin.forms refit

我正在尝试添加具有基本身份验证的静态授权标头,但是当我执行请求时,服务器响应为否定。因此,我尝试以这种方式添加它:

[Headers("Authorization: Basic","Content-Type: application/x-www-form-urlencoded" )]

public interface IRouteApi
{
    [Post("/getRoute?dtxIni={Ini}&dtxFin={Fin}")]
    Task<HttpResponseMessage> getPathInfo(int Ini, int Fin);
}

然后我得到一个静态的Config类:

public static class Config
{
    public static string ApiUrl = "http://www2.baseUrl.it/Base";
    static string username = "aaa";
    static string password = "bbb";
    public static string authHeader = 
    Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));

    public static RefitSettings refitSettings = new RefitSettings()
    {
        AuthorizationHeaderValueGetter = () => Task.FromResult(authHeader)
    };
}
RestService.For<T>(client,Config.refitSettings);

但是它不起作用,请求未经授权。 我也遵循以下问题:Refit and authorization header,但这并不能说服我,因为他/她在他/她的api定义中放置了动态标头。 也许问题出在多个标题中?

1 个答案:

答案 0 :(得分:1)

由于您的授权标头从不更改,因此您实际上不需要使用[ { "section-id": "1", "section-title": "Section 1", "title": "item1" }, { "section-id": "1", "section-title": "Section 1", "title": "item2" }, { "section-id": "2", "section-title": "Section 2", "title": "item3" }, { "section-id": "2", "section-title": "Section 2", "title": "item4" }, { "section-id": "2", "section-title": "Section 2", "title": "item5" } ] (旨在用于更动态的身份验证方案)。相反,只需在AuthorizationHeaderValueGetter上使用DefaultRequestHeaders字典:

HttpClient