C# xamarin表单中的Web API请求错误
我正在尝试使用asp.NETWebAPI和xamarin表单开发一个移动应用程序。得到这样的错误 System.Net.Http.HttpRequestException:发送时出错 请求 我在同一个解决方案中开发了所有移动、Web应用程序。然后使用visual studio 2017通过个人登录创建web api。 这是我的注册绑定模型的源代码 RegisterBinding.csC# xamarin表单中的Web API请求错误,c#,asp.net-mvc,xamarin,asp.net-web-api,xamarin.forms,C#,Asp.net Mvc,Xamarin,Asp.net Web Api,Xamarin.forms,我正在尝试使用asp.NETWebAPI和xamarin表单开发一个移动应用程序。得到这样的错误 System.Net.Http.HttpRequestException:发送时出错 请求 我在同一个解决方案中开发了所有移动、Web应用程序。然后使用visual studio 2017通过个人登录创建web api。 这是我的注册绑定模型的源代码 RegisterBinding.cs public class RegisterBindingModel { public string Em
public class RegisterBindingModel
{
public string Email { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
}
然后我为xamarin绑定创建了viewmodel
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";
}
});
}
}
}
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;
}
}
我还创建了一个api服务来连接移动应用程序和web api服务
ApiService.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";
}
});
}
}
}
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;
}
}
公共异步任务注册表同步(字符串电子邮件、字符串密码、字符串确认密码)
{
尝试
{
var client=新的HttpClient();
var模型=新的注册绑定模型
{
电子邮件=电子邮件,
密码=密码,
ConfirmPassword=ConfirmPassword
};
var json=JsonConvert.serialized对象(模型);
HttpContent _content=新的StringContent(json);
_content.Headers.ContentType=new System.Net.Http.Headers.MediaTypeHeaderValue(“应用程序/json”);
var response=wait client.PostAsync(“http://localhost:54996/api/Account/Register“,_内容);
返回response.IsSuccessStatusCode;
}
捕获(系统异常)
{
投掷;
}
}
最后,我对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>
这是我单击“注册”按钮时出现的错误
由于我对xamarin开发非常陌生,请帮助我找到此问题的解决方案问题最有可能出现在您使用的地址
http://localhost:54996/api/Account/Register
。在模拟器或实际设备上运行时,将其视为外部机器。因此,localhost
将指向该模拟器/设备
检索运行服务器应用程序的计算机的地址,确保它与模拟器/设备连接到同一网络,输入该地址,然后重试
地址很可能以192.168.x.x
或10.x.x.x
开头
另外,
InnerException
中可能有更多细节,最好先检查一下。a。不要使用localhost,b。查看InnerException以获取实际的根本原因System.ObjectDisposedException:无法访问已处置对象。对象名称:“System.Net.Sockets.NetworkStream”。在将localhost更改为ipaddress后,但是这家伙如何获得输出以及如何检查InnerException因为他正在运行UWP应用程序,该应用程序与他的服务器应用程序运行在同一设备上。确定我可以使用ipconfig命令使用我自己系统的ipaddress,该命令就是。并确保允许通过防火墙进行连接。现在,它会引发以下异常:System.ObjectDisposedException:无法访问已处置的对象名称:“System.Net.Sockets.NetworkStream”。系统ip地址为192.168.0.104,移动ip地址为192.168.232.2