Last active
June 11, 2019 22:42
-
-
Save rowanmiller/3f838dc9a0879b8e26f92eec65a07ea6 to your computer and use it in GitHub Desktop.
Trace the chain of redirects for a set of URLs
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
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Xml.Linq; | |
namespace RedirectTracer | |
{ | |
class Program | |
{ | |
private static readonly string _outputFile = "output.csv"; | |
static void Main() | |
{ | |
var urls = GetURLs(); | |
File.WriteAllText(_outputFile, "Page,FinalTarget,FinalStatusCode,TotalRedirects,UrlChain\r\n"); | |
using (var client = new HttpClient(new HttpClientHandler { AllowAutoRedirect = false })) | |
{ | |
foreach (var url in urls) | |
{ | |
var results = GetTarget(url, client).ToArray(); | |
string data = BuildCSV(url, results); | |
File.AppendAllLines(_outputFile, new[] { data }); | |
Console.WriteLine(data); | |
} | |
} | |
} | |
private static IEnumerable<UrlFrame> GetTarget(string url, HttpClient client) | |
{ | |
var response = client.GetAsync(url).Result; | |
yield return new UrlFrame | |
{ | |
Url = response.RequestMessage.RequestUri.ToString(), | |
StatusCode = (int)response.StatusCode | |
}; | |
if ((int)response.StatusCode / 100 == 3) | |
{ | |
var target = response.Headers.Location.IsAbsoluteUri | |
? response.Headers.Location.ToString() | |
: new Uri(response.RequestMessage.RequestUri, response.Headers.Location.ToString()).ToString(); | |
foreach (var item in GetTarget(target, client)) | |
{ | |
yield return item; | |
} | |
} | |
} | |
private static IEnumerable<string> GetURLs() | |
{ | |
// TODO Return starting URLs | |
} | |
private static string BuildCSV(string url, UrlFrame[] results) | |
{ | |
return $"{url}, {results.Last().Url},{results.Last().StatusCode},{results.Count() - 1},{string.Join(" => ", results.Select(f => $"{f.Url} {f.StatusCode}"))}"; | |
} | |
private class UrlFrame | |
{ | |
public string Url { get; set; } | |
public int StatusCode { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment