如何将自己的标头添加到Azure移动服务调用?

问题描述 投票:1回答:2

我有Azure Mobile Service API,我想从Windows Phone应用程序调用它。

所以我使用类似的东西:

public static async Task<bool> InvokeGetUsers()
        {
            Dictionary<string, string> headers = new Dictionary<string, string>();
            headers.Add("X-USER-TOKEN", App.userInfo.token);
            headers.Add("X-ZUMO-APPLICATION", "nxdQEvWOERLaHocwMz");

            Dictionary<string, string> arguments = new Dictionary<string, string>();
            arguments.Add("uuid", "123456");

            if (App.mobileServiceClient != null)
            {
                App.userFriends = await App.mobileServiceClient.InvokeApiAsync<List<GetUsers>>("get_users", System.Net.Http.HttpMethod.Post, arguments);
                return true;
            }
            return false;
        }

我不能做的是将标题信息传递给我的电话,怎么做?

c# azure windows-phone-8 azure-mobile-services
2个回答
1
投票

您可以使用InvokeApiAsync方法的重载版本:

public Task<HttpResponseMessage> InvokeApiAsync(
    string apiName,
    HttpContent content,
    HttpMethod method,
    IDictionary<string, string> requestHeaders,
    IDictionary<string, string> parameters
)

更多信息:https://msdn.microsoft.com/en-us/library/dn268343.aspx


0
投票

理想情况下,您希望通过MobileServiceClient为API的所有调用添加标头。为此,您需要实现一个Http Message Handler并将其传递给MobileServiceClient的构造函数,例如:

App.mobileServiceClient = new MobileServiceClient(apiURI, new MyHandler());

以下是Handler的实现:

public class MyHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        request.Headers.Add("x-api-key", "1234567");
        var response = await base.SendAsync(request, cancellationToken);
        return response;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.