J’ai des problèmes pour appeler la méthode post HttpClient
partir d’une application WP. PostAsync
toujours PostAsync
et ne donne aucune réponse. Le même code fonctionne lorsque je l’essaie à partir d’une application WPF. Voici ce que je fais:
Code API Web du serveur
public class GameController : ApiController { [HttpPost] public GameDto CreateGame(GameDto gameDto) { try { GameManager bl = new GameManager(); gameDto = bl.CreateGame(gameDto); return gameDto; } catch (Exception) { throw; } } }
Code client WP8 appelant depuis la bibliothèque de classes
private async void Button_Click(object sender, RoutedEventArgs e) { try { HttpClient client = new HttpClient(); client.BaseAddress = new Uri("http://localhost:59580"); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); GameDto newGame = new GameDto(); newGame.CreatedBy = 1; newGame.Name = txtGameName.Text; newGame.GameTypeId = (int)cmbGameType.SelectedValue; MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter(); var response = await client.PostAsync("api/Game/CreateGame", newGame, jsonFormatter); response.EnsureSuccessStatusCode(); // Throw on error code. var userDto = await response.Content.ReadAsAsync(); //_products.CopyFrom(products); MessageBox.Show(userDto.Id.ToSsortingng()); } catch (Exception) { throw; } }