2017-09-13 18 views
1

私のXamarinフォームを使用して、WebAPIプロジェクトのコントローラのアクションにデータを送信するPOST要求を作成する作業を進めています。ブレークポイントでのコードは、私が名前空間のSystem.Net.Httpを持っており、コードに記載されているシステムを使用してXamarinフォーム投稿のリクエストHTTPの問題

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

を超えません。

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を送信し、そのアクションにPOSTリクエストを作るための方法とが必要です。

+1

例外が発生していないことは確実ですか? URI文字列 – Jason

+0

にスキーム(「http://」)を追加してみてください。しかしそれはまだ投稿しません。 –

+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 
} 
関連する問題