我编写了以下代码来使用 .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 返回类型总是向我显示:
Id = 1, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"
我如何使用这个库实际执行请求?我在这里做错了什么??
最佳答案
等待结果
var res = test.DoRequest().Result;
即使您的代码是异步的,您也期望立即得到结果。
关于c# - 在 .NET 4.5 + 中使用 HttpClient 执行 XML 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42930133/