简体   繁体   中英

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??

Just simply wait for result

var res = test.DoRequest().Result;

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

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