I want to do a simple HTTP request in C#, but something is not working and all I got is 403 Forbidden
status code.
When I try to do same request in Postman, everything works fine.
I tried to run Fiddler and see all headers that are being sent by Postman. I copy-pasted all of them, but i still got 403 Forbidden
in the request sent by C# code.
C# Code (Using https://flurl.dev):
public static void Main(string[] args)
{
FlurlHttp.Configure(settings => {
settings.HttpClientFactory = new MyClientFactory();
});
var url = "https://example.com"
.AppendPathSegments(new[] { "v1", "oauth", "accesstoken" })
.SetQueryParam("grant_type", "client_credentials")
.AllowAnyHttpStatus()
.WithBasicAuth("username", "password")
.WithHeaders(new {
User_Agent = "Something/0.4.0 Dalvik/2.1.0 (Linux; U; Android 5.1.1; SM-G975F Build/NRD90M)",
X_Secret_Header = "secret_encoded_value",
accept_encoding = "gzip, deflate",
Accept = "*/*"
});
HttpResponseMessage msg = url.GetAsync().Result;
Console.WriteLine("StatusCodeString: " + msg.StatusCode.ToString());
Console.WriteLine();
Console.WriteLine(msg.Content.ReadAsStringAsync().Result);
}
class MyClientFactory : DefaultHttpClientFactory
{
public override HttpMessageHandler CreateMessageHandler()
{
return new HttpClientHandler
{
AllowAutoRedirect = false
};
}
}
C# Request And Response:
Postman Request And Response:
Can someone explain me why is this not working? Same headers, same everything.
I replaced the url with "example.com" because i don't want to show the real API URL here.
Also sorry for so many images.. I don't know how to show the problem here in other way.