✅ ✅ C# HttpClient returning 400, but other tools return 200
I am trying to make a simple web request to a URL using .NET/C# HttpClient. I am getting a 400 error "Bad Request". Whereas everything else such as Python Requests, Postman, and the Web browser dev tools all return a 200 Success.
The URL is http://ericsmithrocks.com. This is a random URL I found that exhibits this behavior. Other URLs seem to work as expected.
I have experimented with replacing "http" with "https" and adding an HttpClientHandler with different settings, with no success.
I am thinking it may be something to do with my headers or an http permanent redirect issue.
Any help would be greatly appreciated. I have attached pictures of all my code / tests. Thanks.
ERIC SMITH
ERIC SMITH
11 Replies
very strange
litteraly the same code version of .NET you are using?
.net 6
For whatever reason, that website wants you to have a User-Agent header:
That's the deal with a couple of websites. From what I heard, back in the day websites claimed that you'd require a specific browser. idk if that was a necessacity because of technical reasons or simply marketing people went ahead and spoofed their user agent
thank you @canton7 , you're a lifesaver.
Can I ask how you figured that out? Did you just happen to know about the weird history that HImmDawg mentioned above?
I've seen exactly this problem before, so it's the first thing I tried
https://webaim.org/blog/user-agent-string-history/ is worth a read
But I don't think it's directly related -- it's just that some servers want to see a user agent, for whatever reason
lol nice. I had been quite stumped for awhile. Don't know if I ever would have solved that naturally on my own. Didn't find anything googling online
https://stackoverflow.com/questions/24274789/is-lack-of-user-agent-in-http-request-valid
The thing to do when you have a working case and a non-working case is to find the difference between them
In this case, you need to work out the difference. You could do that by sniffing the request that each makes (fiddler used to be the go-to tool before they went commercial, not sure what the replacement is), going all the way and using wireshark, or making requests to a server which you can control, e.g. requestbin
Eventually you get it down so that the only difference between the working request and the non-working one is the user-agent, and adding in the user-agent fixes it. Now you know what the problem is
Thanks again. I will read those 2 links
So, this fixed my issue, but I'm wondering why the .NET HttpClient is seemingly defaulting to an empty agent string in the header, when from what I can tell, most other mainstream libraries/languages put a default value in.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.