当找不到响应时,移动服务客户端引发异常
本文关键字:客户端 异常 服务 移动 找不到 响应 | 更新日期: 2023-09-27 17:59:11
我正在尝试将应用程序从azure移动服务移植到azure web应用程序。(移动服务正在运行)。我已经将microsoft帐户身份验证添加到web应用程序中,并且web应用程序api具有MobileAppController属性。我有一个通用的windows应用程序前端,它调用api。该应用程序首先检查数据库中是否有玩家,如果没有,我会得到一个未找到的响应。如果我使用MobileServiceClient使用以下代码调用该方法,我会得到一个异常。
private async Task<HttpResponseMessage> GetAZMAsyncP(string apiext, IDictionary<string,string> param )
{
string myuri = String.Format("{0}{1}", urlbase, apiext);
//客户端是正确登录的MobileServiceClient//我没有得到404未找到的响应,我得到一个异常"请求无法完成,未找到"var响应=等待客户端。InvokeApiAsync(myuri,System.Net.Http.HttpMethod.Get,param);返回响应;}如果我从httpclient调用api并添加自己的头(移动客户端应该为我做这件事),那么我会根据请求得到响应。这是代码:
private async static Task<HttpResponseMessage> GetAZAsync(string apiext)
{
string completeUrl = String.Format("{0}{1}", urlbase, apiext);
// Call out to AZ
using (var http = new HttpClient())
{
// http.BaseAddress = new Uri(completeUrl);
HttpRequestMessage rq = new HttpRequestMessage()
{
RequestUri = new Uri(completeUrl),
Method = HttpMethod.Get
};
addauthheader(rq);
var response = await http.SendAsync(rq);
return response;
}
}
private static void addauthheader(HttpRequestMessage rq)
{
MobileServiceUser user = App.client.CurrentUser;
rq.Headers.Add("X-ZUMO-FEATURES", "AT,QS");
rq.Headers.Add("X-ZUMO-INSTALLATION-ID",
"ff90f37e-0c03-4c52-a343-af711752e383");
rq.Headers.Add("X-ZUMO-AUTH", user.MobileServiceAuthenticationToken);
rq.Headers.Add("Accept", "application/json");
rq.Headers.Add("User-Agent", "ZUMO/2.1");
rq.Headers.Add("User-Agent",
"(lang = Managed; os = Windows Store; os_version = --; arch = X86; version = 2.1.40707.0)");
rq.Headers.Add("X-ZUMO-VERSION",
"ZUMO/2.1(lang = Managed; os = Windows Store; os_version = --; arch = X86; version = 2.1.40707.0)");
rq.Headers.Add("ZUMO-API-VERSION", "2.0.0");
}
你可以试试这个,因为它是现场(和马车)
https://gamenote2.azurewebsites.net/api/Players?displayname=PaulGoldschmidt&teamid=亚利桑那响尾蛇应该给你404分,https://gamenote2.azurewebsites.net/api/Players?displayname=ChaseUtley&teamid=洛杉矶道奇队应该给你一个追逐的对象。(您将被要求登录Microsoft帐户)。
所以我的问题是:1。我可以修复移动客户端调用以获得响应而不是执行吗2.我花这么多时间在这件事上有什么好的理由吗。
如果您检查异常,您会注意到状态代码就在那里——它只是在一个未序列化的属性中。只需在InvokeApiAsync()调用周围添加一个针对StatusCode的try/catch和测试。它应该比为同样的目的编写自己的HTTP客户端代码容易得多。
具体来说,MobileServiceInvalidOperationException
包含失败请求的HttpResponse,因此可以检查exception.Response.StatusCode
的值。