如何将自己的标头添加到Azure Mobile Services调用

本文关键字:Azure Mobile Services 调用 添加 自己的 | 更新日期: 2025-01-25 13:23:28

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

我不能做的是将标头信息传递给我的呼叫,如何做到这一点?

如何将自己的标头添加到Azure Mobile Services调用

您可以使用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

理想情况下,您希望通过MobileServiceClient向所有对API的调用添加一个头。要做到这一点,您需要实现Http消息处理程序,并将其传递给MobileServiceClient的构造函数,例如

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

以下是处理程序的实现:

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