8.5Handling redirect responses

Created on Aug 17, 2026~4 min read

When sending an HTTP remote request, if the target server returns a redirect response (such as 301 Moved Permanently, 302 Found, etc.), the framework follows redirects automatically by default. To handle these situations precisely, refer to the following approaches:

  • Globally enable or disable automatic redirects via HttpRemoteOptions

When sending an HTTP remote request, if the target server returns a redirect response (such as 301 Moved Permanently, 302 Found, etc.), the framework follows redirects automatically by default. To enable or disable this behavior, use the following configuration:

cs
services.AddHttpRemote(builder => {})    .ConfigureOptions(options =>    {        // Sets whether requests should follow redirect responses; default is true        options.AllowAutoRedirect = false;        // Sets the maximum number of redirects a request follows; default is 50        options.MaximumAutomaticRedirections = 50;    });
  • Globally enable or disable automatic redirects via HttpClient

To enable or disable whether HttpClient automatically handles redirects, configure HttpClientHandler or SocketsHttpHandler.

cs
// Configure the default clientservices.AddHttpClient(string.Empty)    .ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler    {        AllowAutoRedirect = true,   // Allow automatic redirects; default is true (allowed)        MaxAutomaticRedirections = 20 // Set the maximum number of redirects    });
  • By handling redirects manually

You can also check the response status code. If the status code is in the 300-399 range, it indicates a redirect status code; in this case you can manually parse the Location header and resend the request using that address.

  1. Disable HttpClient's automatic redirect feature
cs
// Configure the default clientservices.AddHttpClient(string.Empty)    .ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler    {        AllowAutoRedirect = false    });
  1. Manually manage the redirect flow
cs
var responseMessage = await httpRemoteService.GetAsync("https://furion.net/redirect");// Check whether the response message contains a redirect status codevar statusCode = responseMessage.StatusCode;// A while loop is usually used for this check, since multiple redirects may be involvedif (statusCode is HttpStatusCode.Ambiguous or HttpStatusCode.Moved or HttpStatusCode.Redirect or HttpStatusCode.RedirectMethod or HttpStatusCode.RedirectKeepVerb || (int)statusCode == 308){    // Get the redirect address    var redirectUrl = responseMessage.Headers.Location;    if (redirectUrl != null)    {        // Send the request again with the new URL        response = await httpRemoteService.GetAsync(redirectUrl);    }}

Which approach you choose depends on your specific needs. If you need finer-grained control over each redirect process — for example, to log or execute specific logic — manual handling may be the better choice. If you simply want the client to handle all redirects automatically, enabling automatic redirects is more convenient.