Making XML request with HttpClient in .NET 4.5+

I wrote the following code to execute an XML request using the .NET HttpWebClient library as follows:

 public async Task<string> DoRequest()
        {

            using (var httpClient = new HttpClient())
            {
                string requestXML = "My xml here...";
                var request = new HttpRequestMessage(HttpMethod.Post, "example.com");
                request.Content = new StringContent(requestXML, Encoding.UTF8, "text/xml");
                var response = await httpClient.SendAsync(request);
                return await response.Content.ReadAsStringAsync();
            }
        }

      

And in the main function of the console app:

 Klijent test= new Klijent();
 var res = test.DoRequest();

      

But the return type res always shows me the following:

Id = 1, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"

      

How can I make a request using this library? What am I doing wrong here?

+3


source to share


1 answer


Just wait for the result

var res = test.DoRequest().Result;

      



You expect immediate results, even if your code is asynchronous.

+2


source







All Articles