1

Is there anything like this from Java: HTTPClient - Capture a list of all Redirects

for C# HttpClient?

1 Answer 1

3

You can do this using HttpWebRequest:

public static string GetRedirectedUrls(string url)
{
    StringBuilder sb = new StringBuilder();
    while (!string.IsNullOrWhiteSpace(url))
    {
        sb.AppendLine(url);
        HttpWebRequest request = HttpWebRequest.CreateHttp(url);
        request.AllowAutoRedirect = false;
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            url = response.GetResponseHeader("Location");
        }
    }

    return sb.ToString();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.