Last active
September 24, 2024 16:09
-
-
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#]
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
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wanted to share a little piece of code that allows you to get the redirect url of an url using HttpClient