从 SendAsync 方法 ASP.net web API 消息处理程序获取响应数据
Get response data from SendAsync method ASP.net web API Message Handlers
我想从 Web api 获取返回的数据并保存在我的数据库中。
我的消息处理程序代码在这里:
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
Stopwatch watch = new Stopwatch();
TimeSpan second;
watch.Start();
// Call the inner handler.
var response = await base.SendAsync(request, cancellationToken);
watch.Stop();
second = watch.Elapsed;
watch.Reset();
if (second.Seconds > 5)
{
try
{
var req = JsonConvert.SerializeObject(request.GetRouteData());
var data = JsonConvert.SerializeObject(response.Content);
var container = UnityConfig.GetConfiguredContainer();
IPerformanceBal performance = container.Resolve<PerformanceBal>();
performance.SavePerformance(new ApiPerformance()
{
CreatedAt = DateTime.Now,
ExecutionTime = second,
QueryResult = data,
Uri = request.RequestUri.AbsoluteUri
});
}
catch (Exception exception)
{
}
}
return response;
}
我实际上想从响应返回的 "data" 变量中获取数据...类似于“response.data
”...
有什么帮助吗??
可以直接使用HttpContent.ReadAsStringAsync
Method这样的方法访问响应内容...
//...other code
var responseContent = await response.Content.ReadAsStringAsync();
//...other code
从那里您应该可以根据需要使用它。
我想从 Web api 获取返回的数据并保存在我的数据库中。
我的消息处理程序代码在这里:
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
Stopwatch watch = new Stopwatch();
TimeSpan second;
watch.Start();
// Call the inner handler.
var response = await base.SendAsync(request, cancellationToken);
watch.Stop();
second = watch.Elapsed;
watch.Reset();
if (second.Seconds > 5)
{
try
{
var req = JsonConvert.SerializeObject(request.GetRouteData());
var data = JsonConvert.SerializeObject(response.Content);
var container = UnityConfig.GetConfiguredContainer();
IPerformanceBal performance = container.Resolve<PerformanceBal>();
performance.SavePerformance(new ApiPerformance()
{
CreatedAt = DateTime.Now,
ExecutionTime = second,
QueryResult = data,
Uri = request.RequestUri.AbsoluteUri
});
}
catch (Exception exception)
{
}
}
return response;
}
我实际上想从响应返回的 "data" 变量中获取数据...类似于“response.data
”...
有什么帮助吗??
可以直接使用HttpContent.ReadAsStringAsync
Method这样的方法访问响应内容...
//...other code
var responseContent = await response.Content.ReadAsStringAsync();
//...other code
从那里您应该可以根据需要使用它。