在 .NET 4.5 中使用 HttpClient 执行 XML 请求 +

Performing XML Request with HttpClient in .NET 4.5 +

我编写了以下代码来使用 .NET 的 HttpWebClient 库执行 XML 请求,如下所示:

 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();
            }
        }

并且在控制台应用程序的主要函数中:

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

但是 res return 类型总是向我显示:

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

我如何使用这个库实际执行请求?我在这里做错了什么??

只需等待结果

var res = test.DoRequest().Result;

即使您的代码是异步的,您也期望立即得到结果。