SendAsync(httpclient) 失败并出现 InnerException - "The underlying connection was closed: An unexpected error occurred on a send.",但 Fiddler 没有

SendAsync(httpclient) fails with InnerException - "The underlying connection was closed: An unexpected error occurred on a send.", but Fiddler not

我看到了这些 post,但没有成功:

您可以在这里找到完整的代码:https://github.com/te2020/GoEuroAPiClassLibrary/blob/master/GOEuroApi_Requests.cs

只有相关部分(我的想法):

httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate");
            httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0");

            httpClient.BaseAddress = new Uri("https://www.goeuro.com/");


            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "/GoEuroAPI/rest/api/v5/searches");
            request.Content = new StringContent(jsonString,
                                                Encoding.UTF8,
                                                "application/json");//CONTENT-TYPE header

我的 fiddler 看起来像 this 并且可以用。

发送该请求后出现错误:

 var response = await httpClient.SendAsync(request);

我错过了什么?

如果您对请求使用 HTTP(而不是 HTTPS),您会看到来自服务器的更有帮助的响应:

{"message":"Search options are not correct!","statusCode":400}

这似乎表明您发送的搜索选项 JSON 由于某种原因无效。在不知道 goeuro API 的情况下,我无法说出确切的问题所在。不幸的是我没有时间去探究它。

我会做的是使用 Fiddler 'compare' 工具将您的工作请求与来自您的程序的请求进行比较。只需 select Fiddler 中的两个请求并右键单击 -> 比较。然后尝试使您的请求与工作请求完全相同。我注意到工作请求中有您可能没有发送的 cookie,您应该添加它们。