Last active
August 29, 2015 14:15
-
-
Save damianh/a830b56637169be91059 to your computer and use it in GitHub Desktop.
An HttpMessageHandler that automatically follows redirects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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