.net核心2.2 httpclient工厂未提供完整数据作为响应

我正在将.net core 2.2用于我的航班列表应用程序,并且为此使用了wego api。但是,当我使用下面的代码从wego api获取航班时,我没有得到完整的答复,但是在邮递员中,我得到了一个请求的完整结果集。

public async Task<SearchResultMv> GetFlights(FlightParam flightParam,AuthResult auth)
{
    var request = new HttpRequestMessage(HttpMethod.Get,"https://srv.wego.com/metasearch/flights/searches/" + flightParam.SearchId + "/results?offset=0&locale=" + flightParam.locale + "&currencyCode=" + flightParam.currencyCode);
    request.Headers.Add("Bearer",auth.access_token);
    request.Headers.Add("accept","application/json");

    var client = _httpClient.Createclient();
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",auth.access_token);

    var response = await client.SendAsync(request).ConfigureAwait(false);

    SearchResultMv json = new SearchResultMv();

    response.EnsureSuccessStatusCode();

    if (response.IsSuccessStatusCode)
    {
        json = await response.Content.ReadAsAsync<SearchResultMv>().ConfigureAwait(false);
        return json;
    }
}

有时我没有得到以上代码设置的任何结果。 Wego api没有在此api上提供任何分页或过滤。所以请帮助我实现这一目标。感谢前进。

tian5087989 回答:.net核心2.2 httpclient工厂未提供完整数据作为响应

根据他们的文档,您需要轮询其API才能逐渐获得结果。您还需要在返回结果时增加偏移量。

例如,如果第一组结果给您100条结果,则以下请求应将偏移值设置为100。offset=100

文档: https://developers.wego.com/affiliates/guides/flights

编辑-添加了示例解决方案

示例代码每秒轮询API,直到达到所需的结果数量。该代码尚未经过测试,因此您需要根据需要进行调整。

const int numberOfResultsToGet = 100;
var results = new List<SearchResultMv>();

while (results.Count < numberOfResultsToGet)
{
    var response = await GetFlights(flightParam,auth);
    results.AddRange(response.Results);

    // update offset
    flightParam.Offset += response.Results.Count;

    // sleep for 1 second before sending another request
    Thread.Sleep(TimeSpan.FromSeconds(1));
}

更改您的请求以使用动态Offset值。您可以将Offset属性添加到FlightParam类中。

var request = new HttpRequestMessage(
    HttpMethod.Get,$"https://srv.wego.com/metasearch/flights/searches/{flightParam.SearchId}/results?" +
    $"offset={flightParam.Offset}" +
    $"&locale={flightParam.locale}" +
    $"&currencyCode={flightParam.currencyCode}");
本文链接:https://www.f2er.com/3133283.html

大家都在问