Rest HttpClient.SendRequest异常

Rest HttpClient.SendRequest异常,rest,xamarin,xamarin.forms,xamarin.android,httprequest,Rest,Xamarin,Xamarin.forms,Xamarin.android,Httprequest,我使用Xamarin.Forms,在Android模拟器和移动设备上运行我的应用程序 当我尝试发送HttpRequestMessage时,我的应用程序被关闭。这种情况发生在SendRequest方法中,但有时在发送请求时会出现错误 我使用localhost端点: Uri: 我还创建了一个控制台应用程序,在那里测试了请求,一切正常,但Android没有 public async Task<TResult> PostAsync<TResult>(string uri, IRe

我使用Xamarin.Forms,在Android模拟器和移动设备上运行我的应用程序

当我尝试发送HttpRequestMessage时,我的应用程序被关闭。这种情况发生在SendRequest方法中,但有时在发送请求时会出现错误

我使用localhost端点: Uri: 我还创建了一个控制台应用程序,在那里测试了请求,一切正常,但Android没有

public async Task<TResult> PostAsync<TResult>(string uri, IRequestData data, string token = "", string header = "")
        {
            var request = new HttpRequestMessage(HttpMethod.Post, uri);
            request.AddAuthoriationHeader(token);

            var content = data.ToJson();
            request.SetContent(content, ContentTypes.Json);

            var response =  await SendRequestAsync(request);
            var result = await HandleResponseAsync<TResult>(response);

            return result;
        }

public static void SetContent(this HttpRequestMessage request,
            string content,
            ContentTypes contentType)
        {
            request.Content = new StringContent(content,
                Encoding.UTF8,
                contentType.Value());
        }

public static string Value(this ContentTypes contentType)
        {
            switch (contentType)
            {
                case ContentTypes.XxxForm:
                    return "application/x-www-form-urlencoded";
                case ContentTypes.Json:
                    return "application/json";
                default:
                    throw new ArgumentOutOfRangeException(nameof(contentType), contentType, null);
            }
        }

private async Task<HttpResponseMessage> SendRequestAsync(HttpRequestMessage request)
            {
                try
                {
                    using (var client = new HttpClient())
                    {
                        return await client.SendAsync(request); // Shut down on this line
                    }
                }
                catch (ArgumentNullException e)
                {
                    Console.WriteLine(e.Message);
                    throw;
                }
                catch (InvalidOperationException e)
                {
                    Console.WriteLine(e.Message);
                    throw;
                }
                catch (HttpRequestException e)
                {
                    Console.WriteLine(e.Message);
                    throw;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    throw;
                }
            }
它可能是什么?为什么控制台中的本地主机转换工作正常,但手机和模拟器出现错误?

您在地址中实际使用的是本地主机吗

然后您应该将其更改为计算机网络上的IP地址。运行测试控制台应用程序时,您运行的机器与服务器应用程序运行的机器相同,因此localhost仍然是该机器上的localhost

从应用程序运行此应用程序时,即使模拟的设备也将是本地主机,而服务器应用程序不在该设备或模拟器上运行。如果要从设备或模拟器进行测试,请查找开发计算机或服务器应用程序运行的任何其他计算机的IP,并将其用作地址。这个地址可能看起来像:192.168.x.x或10.x.x.x


根据您是在Windows还是Mac上,打开一个控制台窗口并键入ipconfig for Windows和ifconfig for Mac。您应该能够从那里获得您的IP地址。

模拟器的主机将位于IP地址10.0.2.2上。请查看: