2017-09-13 106 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 
}