.NET httpclient 406 不可接受

.NET httpclient 406 not acceptable

我正在尝试使用 .NET 进行 http get 调用,但出现 406 Not accepted 错误。 当我尝试从 PostMan 执行相同的请求时,它工作正常! 我已经尝试验证我的 headers 并将其映射到相同的邮递员请求(包括邮递员的隐藏 auto-generated headers。 也尝试过不同的接受 headers。没有任何效果,有人可以指出我如何识别问题的方向。不幸的是,我无法控制服务器端点。

我的 .NET 控制台应用程序代码是这样的!

            HttpClient httpClient = new HttpClient();
            httpClient.BaseAddress = new Uri(BaseURL + "reports/1.0/");
            httpClient.DefaultRequestHeaders.Add("consumerKey", ConsumerKey);
            httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + AccessToken);
            httpClient.DefaultRequestHeaders.TryAddWithoutValidation("sessionToken ", ott);
            httpClient.DefaultRequestHeaders.Add("User-Agent", "PostmanRuntime/7.29.0");
            httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
            httpClient.DefaultRequestHeaders.Add("Connection", "keep-alive");
            httpClient.DefaultRequestHeaders.Add("Accept", "*/*");

            var response = await httpClient.GetAsync("orders");
            response.EnsureSuccessStatusCode();

来自邮递员控制台的请求如下所示。

来自 Postman 原始日志的相同请求

GET /apim/reports/1.0/orders HTTP/1.1
consumerKey: L3UnAkofqNfW0rhgtYUz7UQzN58a
sessionToken: DA05DA9F178D8C0E5754F03E617A49546330776ABFDDBC
Accept: application/xml
Authorization: Bearer 6971ce62-789e-3b1b-b805-82560f1f21c6
Postman-Token: 04df336c-9226-4eab-8054-c004940e4660

HTTP/1.1 200 OK
Strict-Transport-Security: max-age=15768000; includeSubDomains
Access-Control-Expose-Headers:
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET
Timestamp: 46
Access-Control-Allow-Headers: authorization,Access-Control-Allow-Origin,Content-    Type,SOAPAction,consumerKey,sessionToken,Authorization
Content-Type: application/json
Date: Sun, 22 May 2022 10:57:28 GMT
Transfer-Encoding: chunked

来自Visual Studio 的失败请求似乎在 fiddler 中有 200!下面是 fiddler 中捕获的失败请求的响应。

HTTP/1.1 200 Connection Established
FiddlerGateway: Direct
StartTime: 18:13:22.888
Connection: close

This is a CONNECT tunnel, through which encrypted HTTPS traffic flows.
To view the encrypted sessions inside this tunnel, enable the Tools > Options > HTTPS > Decrypt HTTPS traffic option.

A SSLv3-compatible ServerHello handshake was found. Fiddler extracted the parameters below.

这是我收到的 Visual Studio 错误。

任何关于可能是什么问题或如何解决问题的建议都会有很大帮助!请指教

headerName sessionToken 中似乎有一个 space 导致了这个奇怪的问题。删除 space 解决了问题。

我在一个项目中遇到了同样的问题。这是由于两个同名的api,我没有提到请求方法。这就是为什么服务器无法将请求映射到其中任何一个尝试寻找请求方法的原因。