异步等待 HttpClient.PostAsync 调用
async await for a HttpClient.PostAsync call
我正在尝试包装对 PostAsync 的调用,这样我就不必在整个代码库中重新编码调用序列。我遇到的问题是我从调用中分配的 HttpResponseMessage 与我的方法调用的使用者不同。这是代码:
internal class RestHttpClient
{
public HttpResponseMessage ResponseMessage = new HttpResponseMessage();
public async void SendRequest(string adaptiveUri, string xmlRequest)
{
using (HttpClient httpClient = new HttpClient())
{
StringContent httpConent = new StringContent(xmlRequest, Encoding.UTF8);
try
{
ResponseMessage = await httpClient.PostAsync(adaptiveUri, httpConent);
}
catch (Exception ex)
{
ResponseMessage.StatusCode = HttpStatusCode.InternalServerError;
ResponseMessage.ReasonPhrase = string.Format("RestHttpClient.SendRequest failed: {0}", ex);
}
}
}
}
我正在尝试按如下方式调用该方法:
RestHttpClient restHttpClient = new RestHttpClient();
restHttpClient.SendRequest(adaptiveUri, xmlRequest);
return restHttpClient.ResponseMessage;
当我进行调用时,ResponseMessage 对象始终包含 Ok 状态,即使这不是从 PostAsync 调用实际返回的内容。
你看到这个的原因是因为你的方法是 async void
,它以 "fire and forget" 的方式执行,所以你看到的值 return 是由响应消息,而不是 PostAsync
的响应。
不要公开单个 class HttpResponseMessage
字段,如果在同一实例上同时调用它可能会过时。相反,return 每次一个新实例:
public async Task<HttpResponseMessage> SendRequestAsync(string adaptiveUri, string xmlRequest)
{
using (HttpClient httpClient = new HttpClient())
{
StringContent httpConent = new StringContent(xmlRequest, Encoding.UTF8);
HttpResponseMessage responseMessage = null;
try
{
responseMessage = await httpClient.PostAsync(adaptiveUri, httpConent);
}
catch (Exception ex)
{
if (responseMessage == null)
{
responseMessage = new HttpResponseMessage();
}
responseMessage.StatusCode = HttpStatusCode.InternalServerError;
responseMessage.ReasonPhrase = string.Format("RestHttpClient.SendRequest failed: {0}", ex);
}
return responseMessage;
}
}
当你调用它时,正确地 await
它:
return await restHttpClient.SendRequestAsync(adaptiveUri, xmlRequest);
我正在尝试包装对 PostAsync 的调用,这样我就不必在整个代码库中重新编码调用序列。我遇到的问题是我从调用中分配的 HttpResponseMessage 与我的方法调用的使用者不同。这是代码:
internal class RestHttpClient
{
public HttpResponseMessage ResponseMessage = new HttpResponseMessage();
public async void SendRequest(string adaptiveUri, string xmlRequest)
{
using (HttpClient httpClient = new HttpClient())
{
StringContent httpConent = new StringContent(xmlRequest, Encoding.UTF8);
try
{
ResponseMessage = await httpClient.PostAsync(adaptiveUri, httpConent);
}
catch (Exception ex)
{
ResponseMessage.StatusCode = HttpStatusCode.InternalServerError;
ResponseMessage.ReasonPhrase = string.Format("RestHttpClient.SendRequest failed: {0}", ex);
}
}
}
}
我正在尝试按如下方式调用该方法:
RestHttpClient restHttpClient = new RestHttpClient();
restHttpClient.SendRequest(adaptiveUri, xmlRequest);
return restHttpClient.ResponseMessage;
当我进行调用时,ResponseMessage 对象始终包含 Ok 状态,即使这不是从 PostAsync 调用实际返回的内容。
你看到这个的原因是因为你的方法是 async void
,它以 "fire and forget" 的方式执行,所以你看到的值 return 是由响应消息,而不是 PostAsync
的响应。
不要公开单个 class HttpResponseMessage
字段,如果在同一实例上同时调用它可能会过时。相反,return 每次一个新实例:
public async Task<HttpResponseMessage> SendRequestAsync(string adaptiveUri, string xmlRequest)
{
using (HttpClient httpClient = new HttpClient())
{
StringContent httpConent = new StringContent(xmlRequest, Encoding.UTF8);
HttpResponseMessage responseMessage = null;
try
{
responseMessage = await httpClient.PostAsync(adaptiveUri, httpConent);
}
catch (Exception ex)
{
if (responseMessage == null)
{
responseMessage = new HttpResponseMessage();
}
responseMessage.StatusCode = HttpStatusCode.InternalServerError;
responseMessage.ReasonPhrase = string.Format("RestHttpClient.SendRequest failed: {0}", ex);
}
return responseMessage;
}
}
当你调用它时,正确地 await
它:
return await restHttpClient.SendRequestAsync(adaptiveUri, xmlRequest);