Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rest 如何使用Xamarin.Forms本地Web API和Emulator for Visual Studio进行访问?_Rest_Xamarin_Xamarin.forms_Localhost_Visual Studio Emulator - Fatal编程技术网

Rest 如何使用Xamarin.Forms本地Web API和Emulator for Visual Studio进行访问?

Rest 如何使用Xamarin.Forms本地Web API和Emulator for Visual Studio进行访问?,rest,xamarin,xamarin.forms,localhost,visual-studio-emulator,Rest,Xamarin,Xamarin.forms,Localhost,Visual Studio Emulator,我已经创建了包含身份验证的.NET Framework API,我使用Jetbrains Rider启动了Web API,我使用Visual Studio运行了我的Xamarin.Forms应用程序,我无法从Web API访问任何数据,也无法发布任何数据 Webservice类: private readonly HttpClient _client; public AccountService() { _client = new HttpClient { Ma

我已经创建了包含身份验证的.NET Framework API,我使用Jetbrains Rider启动了Web API,我使用Visual Studio运行了我的Xamarin.Forms应用程序,我无法从Web API访问任何数据,也无法发布任何数据

Webservice类:

private readonly HttpClient _client;

public AccountService()
{
    _client = new HttpClient
    {
        MaxResponseContentBufferSize = 256000
    };
}

public async Task RegisterAsync(string email, string password, string confirmPassword)
{
    var url = "http://169.254.80.80:61348/api/Account/Register";
    var model = new RegisterBindingModel()
    {
        Email = email,
        Password = password,
        ConfirmPassword = confirmPassword
    };

    var json = JsonConvert.SerializeObject(model);

    HttpContent content = new StringContent(json);

    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

    var response = await _client.PostAsync(url, content);
}
登记的开始

private async void Register()
{
    try
    {
        using (UserDialogs.Instance.Loading())
        {
            await _accountServices.RegisterAsync
                (Email, Password, ConfirmPassword);
        }
        UserDialogs.Instance.Alert("Register Successful");

        await _navigation.PushAsync(new LoginPage());
    }
    catch
    {
        UserDialogs.Instance.Alert("Something wrong happened, Try again");
    }
}
我尝试使用以下IP通过Emulator访问本地主机:

  • 10.0.3.2
  • 10.0.2.2
  • 169.254.80.80
我已经尝试了默认网关和本地IP地址,无论是否使用端口,无论使用postman,我都可以完美地使用api

我没有收到错误,但是连接状态代码不成功,我没有得到任何数据,并且新注册的帐户不会发布到api

编辑: 至于答案,我将我的方法改为:

        public async Task<string> RegisterAsync(string email, string password, string confirmPassword)
    {
        var client = new HttpClient()
        {
            BaseAddress = new Uri("http://169.254.80.80:61348/")
        };
        var postData = new List<KeyValuePair<string, string>>();
        var nvc = new List<KeyValuePair<string, string>>();
        nvc.Add(new KeyValuePair<string, string>("email", email));
        nvc.Add(new KeyValuePair<string, string>("password", password));
        nvc.Add(new KeyValuePair<string, string>("confirmPassword", confirmPassword));
        var req = new HttpRequestMessage(HttpMethod.Post, "api/Account/Register") { Content = new FormUrlEncodedContent(nvc) };
        var res = await client.SendAsync(req);
        if (res.IsSuccessStatusCode)
        {
            string result = await res.Content.ReadAsStringAsync();
            string test = JsonConvert.DeserializeObject<string>(result);
            return test;
        }

        return null;
    }
公共异步任务注册表同步(字符串电子邮件、字符串密码、字符串确认密码)
{
var client=新的HttpClient()
{
BaseAddress=新Uri(“http://169.254.80.80:61348/")
};
var postData=新列表();
var nvc=新列表();
添加(新的KeyValuePair(“电子邮件”),电子邮件);
添加(新的KeyValuePair(“密码”,password));
添加(新的KeyValuePair(“confirmPassword”,confirmPassword));
var req=newhttprequestmessage(HttpMethod.Post,“api/Account/Register”){Content=newformurlencodedcontent(nvc)};
var res=wait client.SendAsync(req);
如果(res.IsSuccessStatusCode)
{
字符串结果=wait res.Content.ReadAsStringAsync();
string test=JsonConvert.DeserializeObject(结果);
回归试验;
}
返回null;
}
我使用postman调用web api,如下所示:

我总是喜欢执行web请求,这是我在案例中实现的代码,对我来说非常有用

public static async Task<string> ResgisterUser(string email, string password, string confirmPassword)
{
    var client = new HttpClient(new NativeMessageHandler());
    client.BaseAddress = new Uri("http://192.168.101.119:8475/");
    var postData = new List<KeyValuePair<string, string>>();
    var nvc = new List<KeyValuePair<string, string>>();
    nvc.Add(new KeyValuePair<string, string>("email", email));
    nvc.Add(new KeyValuePair<string, string>("password", password));
    nvc.Add(new KeyValuePair<string, string>("confirmPassword",confirmPassword));
    var req = new HttpRequestMessage(HttpMethod.Post, "api/Vendor/Register") { Content = new FormUrlEncodedContent(nvc) };
    var res = await client.SendAsync(req);
    if (res.IsSuccessStatusCode)
    {
        string result = await res.Content.ReadAsStringAsync();
        string test= JsonConvert.DeserializeObject<string>(result);
        return test;
    }
}
公共静态异步任务ResgisterUser(字符串电子邮件、字符串密码、字符串确认密码)
{
var client=newhttpclient(new NativeMessageHandler());
client.BaseAddress=新Uri(“http://192.168.101.119:8475/");
var postData=新列表();
var nvc=新列表();
添加(新的KeyValuePair(“电子邮件”),电子邮件);
添加(新的KeyValuePair(“密码”,password));
添加(新的KeyValuePair(“confirmPassword”,confirmPassword));
var req=new-HttpRequestMessage(HttpMethod.Post,“api/Vendor/Register”){Content=new-FormUrlEncodedContent(nvc)};
var res=wait client.SendAsync(req);
如果(res.IsSuccessStatusCode)
{
字符串结果=wait res.Content.ReadAsStringAsync();
string test=JsonConvert.DeserializeObject(结果);
回归试验;
}
}

希望它对您有用。

我应该使用哪个IP,您使用的IP对我不起作用。@OmarAlaa它将是您的web api url/Rest api url/localhost,因此,在您的情况下,它将是
http://169.254.80.80:61348
分享您的新代码,以便这里的人可以指导您you@OmarAlaa你能发布你是如何从邮递员那里调用你的webapi的吗?可能有助于人们识别发布数据的正确格式,还可以检查您是否在Android项目清单中提供了internet权限如何在postman中传递参数以注册用户?只需json属性电子邮件和密码以及confirmpassword