简体   繁体   中英

Windows Phone 8.1 - HttpClient SendRequestAsync will never return with a response

i am trying to receive a response from a HttpClient in Windows Phone 8.1 but it hangs on the method:

httpClient.SendRequestAsync(requestMessage);

The code looks like this:

// Create a client
        var httpClient = new HttpClient();

        // Add a new Request Message
        var requestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri(serviceUri));

        // Add our custom headers
        requestMessage.Headers.Add ("Username", username);
        requestMessage.Headers.Add ("Credential", password);
        requestMessage.Headers.Add ("DBName", databaseName);


        requestMessage.Method = HttpMethod.Get;

        // Send the request to the server
        var response = await httpClient.SendRequestAsync(requestMessage);

        // Just as an example I'm turning the response into a string here
        var token = await response.Content.ReadAsStringAsync();


        return token;

The server receives the header values and returns the token but the HttpClient will no get the answer.

Is there anything wrong in this code?

Thank you for your help!

I suspect that further up your call stack, your code is calling Task<T>.Result or Task.Wait . This will deadlock your application.

The appropriate solution is to replace all calls to Result or Wait with await .

More info in my MSDN article on async best practices or my blog post that deals with this kind of async deadlock in detail .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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