Reputation: 189
I have a web api running with base url http://localhost:8082/
I am creating a C# Consol Application which does a corn job and will communicate web api to push some messages using below snippet
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(“http://localhost:8082/”);
var response = client.GetAsync(“api/UpdateMessage/?status=new”).Result;
Console.WriteLine(response.RequestMessage.RequestUri.ToString());
Console.WriteLine(response.StatusCode);
UpdateMessage controller’s Get method is marked with AllowAnonymous attribute.
Problem On running the console application I am getting Unauthorized status code. Even though this call seems not to be reaching the api as I had breakpoints in all places of the web api project.
But what is more confusing is the result of the 1st Console WriteLine of the response.RequestMessage.RequestUri.ToString()
Why my local host request uri is replaced with this url and why the console application is not able to reach the web api which is running via Visual Studio in specified port.
Upvotes: 0
Views: 229
Reputation: 21
You can try to disable auto redirects: https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclienthandler.allowautoredirect?view=net-8.0
using var httpClientHandler = new HttpClientHandler
{
AllowAutoRedirect = false
};
using var httpClient = new HttpClient(httpClientHandler);
Upvotes: 1