Skip to content

Instantly share code, notes, and snippets.

@damianh
Last active August 29, 2015 14:15
Show Gist options
  • Save damianh/a830b56637169be91059 to your computer and use it in GitHub Desktop.
Save damianh/a830b56637169be91059 to your computer and use it in GitHub Desktop.
An HttpMessageHandler that automatically follows redirects
private class AutoFollowRedirectHttpMessageHandler : DelegatingHandler
{
public AutoFollowRedirectHttpMessageHandler(HttpMessageHandler inner) : base(inner)
{ }
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var response = await base.SendAsync(request, cancellationToken);
while(response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.Found)
{
request = new HttpRequestMessage(HttpMethod.Get, response.Headers.Location);
response = await base.SendAsync(request, cancellationToken);
}
return response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment