C# 如何将自己的头添加到Azure移动服务呼叫?
我有Azure移动服务API,我想从Windows Phone应用程序调用它 所以我用了这样的方法:C# 如何将自己的头添加到Azure移动服务呼叫?,c#,azure,windows-phone-8,azure-mobile-services,C#,Azure,Windows Phone 8,Azure Mobile Services,我有Azure移动服务API,我想从Windows Phone应用程序调用它 所以我用了这样的方法: public static async Task<bool> InvokeGetUsers() { Dictionary<string, string> headers = new Dictionary<string, string>(); headers.Add("X-USER-TOKEN",
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;
}
公共静态异步任务InvokeGetUsers()
{
字典头=新字典();
headers.Add(“X-USER-TOKEN”,App.userInfo.TOKEN);
添加(“X-ZUMO-APPLICATION”、“nxdQEvWOERLaHocwMz”);
字典参数=新字典();
添加(“uuid”、“123456”);
如果(App.mobileServiceClient!=null)
{
App.userFriends=wait App.mobileServiceClient.InvokeApiAsync(“get_users”,System.Net.Http.HttpMethod.Post,arguments);
返回true;
}
返回false;
}
我不能做的是将标题信息传递给我的调用,如何做到这一点?InvokeApiAsync方法有一个重载版本,您可以使用:
public Task<HttpResponseMessage> InvokeApiAsync(
string apiName,
HttpContent content,
HttpMethod method,
IDictionary<string, string> requestHeaders,
IDictionary<string, string> parameters
)
public任务调用iasync(
字符串名称,
HttpContent内容,
HttpMethod方法,
IDictionary请求头,
索引参数
)
此处的更多信息:理想情况下,您希望通过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;
}
}
公共类MyHandler:DelegatingHandler
{
受保护的覆盖异步任务SendAsync(HttpRequestMessage请求,CancellationToken CancellationToken)
{
添加(“x-api-key”、“1234567”);
var response=await base.sendaync(请求、取消令牌);
返回响应;
}
}
我应该使用哪种类型的HttpContent-因为上面的版本不需要指定它。因为它是get请求,所以您不应该发送任何内容,I。e将其设置为null。