Skip to content

Instantly share code, notes, and snippets.

@Delaire
Last active April 17, 2023 20:51
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Delaire/3f1283dc6365d705dfd6ba24498d4993 to your computer and use it in GitHub Desktop.
Save Delaire/3f1283dc6365d705dfd6ba24498d4993 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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