【问题标题】:How to Access using Xamarin.Forms local Web API with Emulator for Visual Studio?如何使用 Xamarin.Forms 本地 Web API 和 Emulator for Visual Studio 进行访问?
【发布时间】:2018-07-31 21:05:44
【问题描述】:

我创建了包含身份验证的 .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 地址,无论是否使用端口,无论使用邮递员,我都可以完美地使用我的 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;
    }

我使用 postman 调用 web api,如下所示: http://localhost:61348/api/Account/Register

【问题讨论】:

  • postman中如何传递参数来注册用户?
  • 只是 json 属性电子邮件和密码以及确认密码

标签: rest xamarin xamarin.forms localhost visual-studio-emulator


【解决方案1】:

我总是更喜欢Newtonsoft Json.NET 来执行网络请求,这是我在我的案例中实现的代码,它对我很有用。

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

希望它对你有用。

【讨论】:

  • 我应该使用哪个IP,您使用的IP对我不起作用。
  • @OmarAlaa 这将是你的 web api url / Rest Api url / localhost,所以在你的情况下它将是 http://169.254.80.80:61348
  • 分享您的新代码,以便这里的人可以指导您
  • @OmarAlaa 你能发布你如何从邮递员那里调用你的 webapi 吗?可能会帮助人们识别发布数据的正确格式
  • 还要检查您是否在您的 Android 项目清单中提供了互联网权限
猜你喜欢
  • 2017-04-22
  • 2015-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-01
  • 1970-01-01
相关资源
最近更新 更多