1

我想从 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;
}

我实际上想从响应返回的“数据”变量中获取数据......类似于“ response.data”......对此有任何帮助吗?

4

1 回答 1

1

HttpContent.ReadAsStringAsync您可以像这样使用 Method 方法直接访问响应内容...

//...other code

var responseContent = await response.Content.ReadAsStringAsync();

//...other code

从那里你应该能够根据需要使用它。

于 2017-01-03T04:25:01.917 回答