2017-09-13 145 views
1

我正在使用我的Xamarin表单将数据发送到我的控制器中的Action在我的WebAPI项目中的POST请求。带断点的代码不会超越Xamarin表单发布请求Http问题

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

我有命名空间System.Net.Http和using代码中提到的系统。

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

需要一种方法来使POST请求到行动和发送该字符串或Model.Email作为参数。

+1

你确定它没有抛出异常吗?尝试添加一个方案(“http://”)到URI字符串 – Jason

+0

这似乎有所帮助!但它仍然不会发布。 –

+0

但问题是什么?你有异常,一些消息等? – Eru

回答

1

您需要使用正确的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 
}