Xamarin表格发布请求HTTP问题



我正在努力使用我的Xamarin表单进行发布请求,以将数据发送到我的WebAPI项目中的控制器中的操作。带有断点的代码不会超越

client.BaseAddress = new Uri("192.168.79.119:10000");

我有命名空间system.net.http和代码中提到的系统。

 private void BtnSubmitClicked(object sender, EventArgs eventArgs)
    {
        System.Threading.Tasks.Task<HttpResponseMessage> statCode = ResetPassword();
        App.Log(string.Format("Status Code", statCode));

    }
    public async Task<HttpResponseMessage> ResetPassword()
    {
        ForgotPassword model = new ForgotPassword();
        model.Email = Email.Text;
        var client = new HttpClient();
        client.BaseAddress = new Uri("192.168.79.119:10000");
        var content = new StringContent(
           JsonConvert.SerializeObject(new { Email = Email.Text }));
        HttpResponseMessage response = await client.PostAsync("/api/api/Account/PasswordReset", content); //the Address is correct
        return response;
    }

需要一种方法来向该操作提出发布请求,并将该字符串或Model.Email作为参数发送。

您需要使用适当的URI,还需要await从称为方法返回的任务。

private async void BtnSubmitClicked(object sender, EventArgs eventArgs) {
    HttpResponseMessage response = await ResetPasswordAsync();
    App.Log(string.Format("Status Code: {0}", response.StatusCode));
}
public Task<HttpResponseMessage> ResetPasswordAsync() {
    var model = new ForgotPassword() {
        Email = Email.Text
    };
    var client = new HttpClient();
    client.BaseAddress = new Uri("http://192.168.79.119:10000");
    var json = JsonConvert.SerializeObject(model);
    var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
    var path = "api/api/Account/PasswordReset";
    return client.PostAsync(path, content); //the Address is correct
}

最新更新