Different HttpClient headers

When I make an HTTP request in my browser (Chrome), I get different response headers than I do in my program using HttpClient with a HttpClientHandler I'm using all the exact same headers, including the same cookies, and making the request to the same URL - I also have redirects disabled Why could this be? Chrome shows me 11 headers, whereas C#'s HttpClient only shows me 6 headers from the exact same request
10 Replies
leowest
leowest3mo ago
unhappily its really hard to guess without code
Angius
Angius3mo ago
Are you spoofing the Chrome user agent as well?
leowest
leowest3mo ago
should if he is sending the same headers as he said
UnemployedNinja
UnemployedNinja3mo ago
c#
HttpClientHandler handler = new HttpClientHandler();
handler.AllowAutoRedirect = false;
if (proxy != null) handler.Proxy = proxy;

using (HttpClient client = new HttpClient(handler, true))
using (HttpResponseMessage response = await client.GetAsync($"https://us.battle.net/")) {

client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br, zstd");
// ... More headers, including the cookies ...

if (response.StatusCode == HttpStatusCode.Found) {
HttpResponseHeaders headers = response.Headers;

var headersDict = headers.ToDictionary(a => a.Key, a => a.Value);

Console.WriteLine("\nHeaders:");
foreach (var (key, value) in headersDict) {
string valStr = "";
foreach (string val in value) { valStr += val; }
Console.WriteLine($" {key}: {valStr}");
}
}

}
c#
HttpClientHandler handler = new HttpClientHandler();
handler.AllowAutoRedirect = false;
if (proxy != null) handler.Proxy = proxy;

using (HttpClient client = new HttpClient(handler, true))
using (HttpResponseMessage response = await client.GetAsync($"https://us.battle.net/")) {

client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br, zstd");
// ... More headers, including the cookies ...

if (response.StatusCode == HttpStatusCode.Found) {
HttpResponseHeaders headers = response.Headers;

var headersDict = headers.ToDictionary(a => a.Key, a => a.Value);

Console.WriteLine("\nHeaders:");
foreach (var (key, value) in headersDict) {
string valStr = "";
foreach (string val in value) { valStr += val; }
Console.WriteLine($" {key}: {valStr}");
}
}

}
Yes
leowest
leowest3mo ago
you're adding the headers after u made the request it needs to be done before
UnemployedNinja
UnemployedNinja3mo ago
omfg ok.......................
leowest
leowest3mo ago
you add your headers to the HttpClientHandler
UnemployedNinja
UnemployedNinja3mo ago
ty lol I'm so dumb
leowest
leowest3mo ago
and that's why posting code = quick job
UnemployedNinja
UnemployedNinja3mo ago
An hour of my life................... ty lol
Want results from more Discord servers?
Add your server
More Posts
HttpRequestException:An error occurred while sending the request. WebExceptionHttpRequestException:An error occurred while sending the request. WebException:Unable to connect to ✅ EF does not put datetimes properly to the datebaseI am writing a backend app in asp.net core. This is my service code : ```c# public virtual async TaShadow Prop Error with IdentityDbContextThis is my IdentityDbContext: public class ApplicationUser : IdentityUser { [Required(ErrorMessWpf custom button template probleHi! I am trying to create custom button control, but I have a problem with a content property. ButtoWinfoms problem in Hosts fileI am writing a mini site blocker, the program has 2 functions to block the site and add the site thaDTO validation with DRY principle (without repeating the same validation)Hello, I would like to know what is used in practice, regarding the DTO validation. Let's say for 2 I'm using fedora, and i want create some .NET formsWhat should i do to create projects with ASP.NET forms in Linux system(Fedora)?✅ My API returns 500 internal server error with the SQL exception text, why?Hello every one, I built an ASP.NET API in .NET and I wrote a controller Action that looks like thiAdvice on how to correctly integrate more complex Javascript codeHey, I've developed an ASP.NET app before, but that one was pretty much all about the backend and t✅ A generic error occurred in GDI+.' -> Save Image PathHey, I been trying to making some face recognition using EMGU.CV, but I faces a problem which, when