简体   繁体   English

Windows手机HttpClient PostAsync挂起,没有响应

[英]Windows phone HttpClient PostAsync hang with no response

I am having problem in calling the HttpClient post method from WP application.The PostAsync always hangs and does not give any response.The same code works when i try it from WPF application. 我在从WP应用程序调用HttpClient post方法时遇到问题PostAsync总是挂起并且没有给出任何响应。当我从WPF应用程序尝试它时,相同的代码工作。 Here is what I am doing: 这是我在做的事情:

Server Web API code 服务器Web API代码

public class GameController : ApiController
{
[HttpPost]

public GameDto CreateGame(GameDto gameDto)
        {
            try
        {
            GameManager bl = new GameManager();
            gameDto = bl.CreateGame(gameDto);
            return gameDto;
        }
        catch (Exception)
        {

            throw;
        }
    }
}

Client WP8 code calling from class library 客户端WP8代码从类库调用

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<GameDto>("api/Game/CreateGame", newGame, jsonFormatter);
            response.EnsureSuccessStatusCode(); // Throw on error code.
            var userDto = await response.Content.ReadAsAsync<GameDto>();
            //_products.CopyFrom(products);
            MessageBox.Show(userDto.Id.ToString());
        }
        catch (Exception)
        {

            throw;
        }

    }

Checkout This Answer res.olved my issue. 结帐本答案重新解决了我的问题。 Use ConfigureAwait 使用ConfigureAwait

var result = await httpClient.GetStreamAsync("weeklyplan")
                         .ConfigureAwait(continueOnCapturedContext:false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM