Instantly share code, notes, and snippets.

Embed
What would you like to do?
UWP, C# - Retrieve the redirect url using HttpClient from the Headers of the Response - [HttpClient,C#]
public static class CoreTools
{
public static async Task<string> GetRedirectedUrl(string url)
{
//this allows you to set the settings so that we can get the redirect url
var handler = new HttpClientHandler()
{
AllowAutoRedirect = false
};
string redirectedUrl = null;
using (HttpClient client = new HttpClient(handler))
using (HttpResponseMessage response = await client.GetAsync(url))
using (HttpContent content = response.Content)
{
// ... Read the response to see if we have the redirected url
if (response.StatusCode == System.Net.HttpStatusCode.Found)
{
HttpResponseHeaders headers = response.Headers;
if (headers != null && headers.Location != null)
{
redirectedUrl = headers.Location.AbsoluteUri;
}
}
}
return redirectedUrl;
}
}
@Delaire

This comment has been minimized.

Show comment
Hide comment
@Delaire

Delaire Mar 22, 2018

I wanted to share a little piece of code that allows you to get the redirect url of an url using HttpClient

Owner

Delaire commented Mar 22, 2018

I wanted to share a little piece of code that allows you to get the redirect url of an url using HttpClient

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment