简体   繁体   English

使用.NET Core而不是.NET Framework进行400 BadRequest响应

[英]400 BadRequest response with .NET Core but not .NET Framework

I had the link http://www.swensens.com.vn/#huong-vi-tinh-yeu which I need to validate that it's still working or not by request and getting the StatusCode. 我有链接http://www.swensens.com.vn/#huong-vi-tinh-yeu ,我需要通过请求验证它是否仍在工作并获取StatusCode。

It's simple like that 这很简单

var client = new HttpClient();
var result = await client.GetAsync(@"http://www.swensens.com.vn/#huong-vi-tinh-yeu");           
Console.Write(result.StatusCode);

When I tested with .NET Core 2.1, it gets 400 Bad Request. 当我使用.NET Core 2.1进行测试时,它会获得400 Bad Request。 I suspect the "#huong-vi-tinh-yeu" may cause the problem because I received 200 OK without them. 我怀疑“#huong-vi-tinh-yeu”可能会导致问题,因为我收到200 OK而没有它们。 But when I tested it again in .NET Framework, the full link is perfectly getting 200 OK with no problem occurred. 但是,当我在.NET Framework中再次测试它时,完整链接完全可以获得200 OK,没有出现任何问题。

I also tested by using 3rd library such as Flurl, RestSharp in .NET Core but the results are only one with 400 Bad Request. 我还测试了使用第三个库,例如.NET Core中的Flurl,RestSharp,但结果只有400个Bad Request。

Another testing on the website https://httpstatus.io still 200 OK 网站上的另一个测试https://httpstatus.io仍然200 OK 1

I want to keep .NET Core because my current Azure Function system is written through .NET Core. 我想保留.NET Core,因为我当前的Azure Function系统是通过.NET Core编写的。

So what is the problem is? 那么问题是什么?

That is a known bug. 这是一个已知的错误。 I reported that bug some time ago on the Github repository: [2.1] Redirect with location having an anchor fails in a HTTP 400 on some servers #34728 我前段时间在Github存储库上报告了这个bug: [2.1]在某些服务器上的HTTP 400中,带有锚点的位置重定向失败#34728

It seems this bug was introduced in .NET Core 2.1, and fixed in .NET Core 3. 看来这个bug是在.NET Core 2.1中引入的,并在.NET Core 3中修复。

As a temporary workaround, you can call this before you make any HTTP requests, which will disable the faulting code: 作为临时解决方法,您可以在发出任何HTTP请求之前调用此方法,这将禁用错误代码:

AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM