我正在创建http客户端以使用一些api。
这是我的客户端方法调用api端点。
public async Task<HttpResponseMessage> SendRequestAsync()
{
string adaptiveUri = "https://some-api/api/Authentication/AuthenticateThirdPartyUserAsync";
using (HttpClient httpClient = new HttpClient())
{
var json = JsonConvert.SerializeObject(new { userName = "uname", password = "123", applicantCode = "hello" });
var payload = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage responseMessage = null;
try
{
responseMessage = await httpClient.PostAsync(adaptiveUri, payload);
}
catch (Exception ex)
{
if (responseMessage == null)
{
responseMessage = new HttpResponseMessage();
}
responseMessage.StatusCode = HttpStatusCode.InternalServerError;
responseMessage.ReasonPhrase = string.Format("RestHttpClient.SendRequest failed: {0}", ex);
}
return responseMessage;
}
}
调用方法如下
public async Task<IBaseStatus> Handle(InspectionAddedEvent domainEvent)
{
var tk = await _iAClient.SendRequestAsync();
return something;
}
但是await
不会等到后同步完成。
但是当我使用时
httpClient.PostAsync(adaptiveUri, payload).GetAwaiter().GetResult()
等待发布完成。
任何人对此都有想法吗?
谢谢。
[await _iAClient.SendRequestAsync();
绝对等待PostAsync
。