改装/ HttpClient / ModernHttpClient - 400错误请求

时间:2016-11-07 20:42:48

标签: c# httpclient reactiveui reactive refit

最近开始使用ReactiveUI并且到目前为止一直很好。在试图找出问题时,我遇到了HttpClient / Refit问题。我有一个很长的列表,该列表中的每个项目都必须按如下方式向API发出请求。

这是一个ReactiveObject

public SearchResult()
{
    LoadStats = ReactiveCommand.CreateAsyncTask(_ => DownloadMultipleCalls(this.IdText, this.DisplayText));

    LoadStats
        .ThrownExceptions
        .SubscribeOn(RxApp.MainThreadScheduler)
        .Subscribe(ex => UserError.Throw("Couldn't load stats", ex));

    _avaText = LoadStats.Select(x => x.Ava).ToProperty(this, x => x.AvaText, string.Empty);
    _perText = LoadStats.Select(x => x.Per).ToProperty(this, x => x.PerText, string.Empty);

    this.WhenAnyValue(x => x.IdText)
        .Where(x => !string.IsNullOrWhiteSpace(x))
                //.Select(_ => Observable.Timer(DateTimeOffset.MinValue, TimeSpan.FromSeconds(1), RxApp.MainThreadScheduler))
        .Retry(5)
        .InvokeCommand(LoadStats);
}

在DownloadMultipleCalls中发生以下情况。

async static Task<AvaPer> DownloadMultipleCalls(string id, string displaytext)
{
    AvaPer response = new AvaPer();
    var block = new ActionBlock<string>(async service =>
    {
        response = await DownloadCall(service, displaytext);
    },
    new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 4 });

    block.Post(id);

    block.Complete();
    await block.Completion;

    return response;
}

async static Task<AvaPer> DownloadCall(string id, string displaytext)
{
    System.Diagnostics.Debug.WriteLine($"Starting download at {DateTimeOffset.Now}");

    var client = new HttpClient(NetCache.UserInitiated)
    {
        BaseAddress = new Uri("https://api.adres.com"),
    };

    //client.Timeout = TimeSpan.FromMilliseconds(10);

    var token = GetToken;

    client.DefaultRequestHeaders.Add("authToken", token);
    var api = RestService.For<IMyApi>(client);

    AvaPer idEntries = new AvaPer();
    try
    {
        var searchResult = await api.GetAvaPerStatusRaw(id); // <== Here is the error (400 - Bad Request)

        //var parsedEntries = Task.Run(() => {
        idEntries = new AvaPer
        {
            Id = searchResult.Id.ToString() ?? string.Empty,
            display = displaytext ?? string.Empty,
            Ava = searchResult.AvailabilityStatus ?? string.Empty,
            Per = searchResult.PerformanceStatus ?? string.Empty,
            Updated = DateTimeOffset.Now.ToLocalTime().ToString() ?? string.Empty
        };
        //}).ConfigureAwait(false);

        //return idEntries;
    }
    catch (ApiException ex)
    {
        var statusCode = ex.StatusCode;
        var error = ex.GetContentAs<Models.ServiceModel.ErrorResponse>();
    }

    return idEntries;
}

最后,这行代码错误var searchResult = await api.GetAvaPerStatusRaw(id);应用程序因响应“400错误请求”而崩溃,如果我查看错误,则表明GET请求中没有id值。但是当我检查id的初始值时,有一个值,所以它不应该响应。谁能帮我这个?如果您有任何疑问,请随时提出。非常感谢你。

亲切的问候,

费尔南多

0 个答案:

没有答案