简体   繁体   中英

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.

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. I suspect the "#huong-vi-tinh-yeu" may cause the problem because I received 200 OK without them. But when I tested it again in .NET Framework, the full link is perfectly getting 200 OK with no problem occurred.

I also tested by using 3rd library such as Flurl, RestSharp in .NET Core but the results are only one with 400 Bad Request.

Another testing on the website https://httpstatus.io still 200 OK 1

I want to keep .NET Core because my current Azure Function system is written through .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

It seems this bug was introduced in .NET Core 2.1, and fixed in .NET Core 3.

As a temporary workaround, you can call this before you make any HTTP requests, which will disable the faulting code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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