【问题标题】:Web API request error in xamarin formsxamarin 表单中的 Web API 请求错误
【发布时间】:2018-10-15 00:02:33
【问题描述】:

我正在尝试使用 asp.net web api 和 xamarin 表单开发移动应用程序。遇到这样的错误

System.Net.Http.HttpRequestException:发送时出错 请求

我在同一个解决方案中开发了所有应用程序 Mobile、Web。然后使用 Visual Studio 2017 创建具有个人登录的 Web api。 这是我的RegisterBinding模型源代码

RegisterBinding.cs

public class RegisterBindingModel
{
    public string Email { get; set; }

    public string Password { get; set; }

    public string ConfirmPassword { get; set; }
}

然后我为 xamarin 绑定创建了视图模型 RegisterViewModel.cs

    class RegisterViewModel
    {
    ApiServices _apiserv = new ApiServices(); 
    public string Email { get; set; }
    public string Password { get; set; }
    public string ConfirmPassword { get; set; }
    public string Message { get; set; }

    public ICommand RegisterCommand {
        get
        {
            return new Command(async() =>
            {
                var isSucess = await _apiserv.RegisterAsync(Email, Password, ConfirmPassword);
                if (isSucess)
                {
                    Message = "Sucessfully Registered!";
                }
                else
                {
                    Message = "Try again";
                }
            });
        }
    }
}

我还创建了 apiservice 来连接移动应用程序和 web api 服务

ApiService.cs

public async Task<bool> RegisterAsync(string email, string password, string confirmPassword)
        {
            try
            {
                var client = new HttpClient();

                var model = new RegisterBindingModel
                {
                    Email = email,
                    Password = password,
                    ConfirmPassword = confirmPassword
                };

                var json = JsonConvert.SerializeObject(model);

                HttpContent _content = new StringContent(json);

                _content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");


                var response = await client.PostAsync("http://localhost:54996/api/Account/Register", _content);
                return response.IsSuccessStatusCode;

            }
            catch (System.Exception)
            {

                throw;
            }


        }

最后我对 xamarin xaml 的看法是

RegisterPage.cs

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="CIDSCONMOB.Views.RegisterPage"
            xmlns:vm="clr-namespace:CIDSCONMOB.ViewModels">

    <ContentPage.BindingContext>
        <vm:RegisterViewModel/>
    </ContentPage.BindingContext>

    <StackLayout Orientation="Vertical">
        <Entry Text="{Binding Email}"
               Placeholder="Email"/>
        <Entry Text="{Binding Password}"
               Placeholder="Password"
               IsPassword="True"/>
        <Entry Text="{Binding ConfirmPassword}"
               Placeholder="Confirm Password"
               IsPassword="True"/>
        <Button Command="{Binding RegisterCommand}"
                 Text="Sign Up"/>
        <Label Text="{Binding Message}"/>
    </StackLayout>

</ContentPage>  

这是我点击注册按钮时的错误

HTTP Exception

由于我是 xamarin 开发的新手,请帮助我找到解决此问题的方法

【问题讨论】:

  • 一个。不要使用本地主机,b。查看 InnerException 以获得实际的根本原因
  • System.ObjectDisposedException:无法访问已处置的对象。对象名称:“System.Net.Sockets.NetworkStream”。将 localhost 更改为 ipaddress 后

标签: c# asp.net-mvc xamarin asp.net-web-api xamarin.forms


【解决方案1】:

问题很可能出在您使用的地址http://localhost:54996/api/Account/Register。在模拟器或实际设备上运行时,将其视为外部机器。因此,localhost 将指向该模拟器/设备。

检索运行服务器应用程序的机器的地址,确保它与您的模拟器/设备连接到同一网络,输入地址并重试。

地址很可能以192.168.x.x10.x.x.x 开头。

另外,InnerException 中可能有更多细节,最好先检查一下。

【讨论】:

  • 但是这些家伙如何获得输出youtube.com/watch?v=t0v4srA4NhA 以及如何检查内部异常
  • 因为他正在运行 UWP 应用程序,该应用程序与他的服务器应用程序在同一设备上运行
  • 好的,我可以通过 ipconfig 命令使用我自己系统的 ipaddress
  • 应该是那个。并确保允许通过您的防火墙进行连接。
  • 现在它抛出此异常:System.ObjectDisposedException:无法访问已处置的对象对象名称:'System.Net.Sockets.NetworkStream'。系统ip地址为192.168.0.104,手机ip为192.168.232.2
猜你喜欢
  • 1970-01-01
  • 2020-11-13
  • 2016-06-10
  • 1970-01-01
  • 2019-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多