2016-07-28 22 views
0

我試圖將應用程序從天藍色的移動服務移植到天藍色的Web應用程序。 (移動服務正在工作)。我已將微軟帳戶身份驗證添加到Web應用程序,並且Web應用程序API具有MobileAppController屬性。我有一個調用API的通用Windows應用程序前端。應用程序首先檢查一個玩家是否在數據庫中,如果沒有,我會得到一個未找到的響應。如果我使用MobileServiceClient使用以下代碼調用方法,則會發生異常。移動服務客戶端在未找到響應時拋出異常

private async Task<HttpResponseMessage> GetAZMAsyncP(string apiext, IDictionary<string,string> param) 
    { 
     string myuri = String.Format("{0}{1}", urlbase, apiext); 

//客戶端是在 正確記錄的MobileServiceClient //我沒有得到響應是404沒有找到,我得到一個異常「的請求無法完成,未找到」 VAR響應= await client.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=Paul施密特& teamid =亞利桑那州響尾蛇 應該給你一個404, https://gamenote2.azurewebsites.net/api/Players?displayname=Chase厄特利& teamid =洛杉磯 - 洛杉磯 - 道奇 應該給你一個蔡斯·阿特利對象。 (YOu將被要求登錄到Microsoft帳戶)。

所以我的問題:1.我可以修復mobileclient調用來獲得響應而不是執行 2.是否有任何理由讓我花這麼多時間在這個上。

回答

1

如果你檢查異常,你會注意到狀態碼在那裏 - 它只是在一個沒有序列化的屬性中。只需用try/catch包圍InvokeApiAsync()調用並測試StatusCode即可。它應該比爲自己的目的編寫自己的HTTP客戶端代碼容易得多。

具體而言,MobileServiceInvalidOperationException包含失敗請求的HttpResponse,因此您可以檢查exception.Response.StatusCode值。

+0

上面的工作是可以的,但原則上。爲什麼當我要求httpresponse時應該得到異常?在我看來,直覺得到異常。 –

相關問題