2016-08-12 41 views
0

嗨,我使用下面的代碼,它總是給出響應,找不到服務器,但是當我從控制檯表單測試相同的工作。UWP表單如何在Xmarin PCL項目中進行Http調用?

using (Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient()) 
     { 
      Uri url = new Uri(Constant.DATABASE_LIST_API); 
      Windows.Web.Http.HttpRequestMessage httpRequest = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Get, url); 
      Task< Windows.Web.Http.HttpResponseMessage> responseAsync = client.SendRequestAsync(httpRequest).AsTask(); 
      responseAsync.Wait(); 
      responseAsync.Result.EnsureSuccessStatusCode(); 

      Task<Windows.Storage.Streams.IBuffer> asyncBuffer = responseAsync.Result.Content.ReadAsBufferAsync().AsTask(); 
      asyncBuffer.Wait(); 
      byte[] resultByteArray = asyncBuffer.Result.ToArray(); 
      string responseString = Encoding.UTF8.GetString(resultByteArray, 0, resultByteArray.Length); 

      responseAsync.Result.Dispose(); 
     } 

回答

0

你在Constant.DATABASE_LIST_API中使用「localhost」嗎?

如果是這樣,請嘗試IP地址。如果您在模擬器中運行您的應用程序,它將無法找到本地主機,如果您的web服務正在本地機器上運行,因爲仿真器是不同的設備。

+0

:) Markus,它實際上我沒有看到設備中的無線連接。 –

+0

你是什麼意思?它現在工作嗎? –