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

时间:2015-03-25 14:15:33

标签: c# azure windows-phone-8 azure-mobile-services

我有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;
        }

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

理想情况下,您希望通过MobileServiceClient为所有调用添加标头。为此,您需要实现 Http消息处理程序,并将其传递给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;
    }
}