UWP如何在Xmarin PCL项目中进行Http调用?

时间:2016-08-12 07:58:37

标签: c# xamarin uwp-xaml

您好我正在使用以下代码,它总是给出响应服务器找不到但我从控制台测试它的工作情况。

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

1 个答案:

答案 0 :(得分:0)

你在Constant.DATABASE_LIST_API中使用“localhost”吗?

如果有,请尝试IP地址。如果您在仿真器中运行应用程序,如果您的Web服务在本地计算机上运行,​​则无法找到本地主机,因为仿真器是另一台设备。