Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing XML Request with HttpClient in .NET 4.5 +

I have written a following code to perform an XML request using .NET's HttpWebClient library like this:

 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 application:

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

But the res return type is always showing me this:

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

How do I actually perform the request with this library? What am I doing wrong here??

like image 350
User987 Avatar asked Mar 13 '26 03:03

User987


1 Answers

Just simply wait for result

var res = test.DoRequest().Result;

You are expecting immediate result, even though you code is asynchronous.

like image 92
Mantas Čekanauskas Avatar answered Mar 14 '26 18:03

Mantas Čekanauskas