Last active
January 2, 2019 06:40
-
-
Save techthoughts2/95d550e02c4c43914764639eb1eaddcc to your computer and use it in GitHub Desktop.
Various ways to handle redirects
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
<# | |
try { | |
$a = Invoke-WebRequest -Uri $uri -MaximumRedirection 0 -ErrorAction Stop | |
}#try_Invoke-WebRequest | |
catch { | |
#if($_.ErrorDetails.Message -like "*maximum redirection*"){ | |
if($_.Exception.Message -like "*Moved*"){ | |
Write-Verbose -Message 'Moved detected.' | |
#$result = $a.Headers.Location | |
$result = $a.Exception.Response.Headers.Location.AbsoluteUri | |
}#if_Error_Moved | |
else{ | |
Write-Warning -Message 'An Error was encountered resolving a potential shortlink:' | |
Write-Error $_ | |
}#else_Error_Moved | |
}#catch_Invoke-WebRequest | |
return $result | |
#> | |
<# | |
Write-Verbose -Message 'Creating HttpWebRequest' | |
$wc = [System.Net.HttpWebRequest]::Create($Uri) | |
Write-Verbose -Message 'Created.' | |
$wc.AllowAutoRedirect = $false | |
try { | |
Write-Verbose -Message 'Resolving URI...' | |
$response = $wc.GetResponse() | |
Write-Verbose -Message 'Resolved.' | |
if ($response.Headers["Location"]) { | |
Write-Verbose -Message 'Location detected.' | |
$Uri = $response.Headers["Location"] | |
}#if_location | |
else { | |
Write-Verbose -Message 'No location detected. Null return.' | |
$Uri = $null | |
}#else_location | |
}#try_HttpWebRequest | |
catch { | |
Write-Verbose -Message 'Error resolving. Evaluating conditions...' | |
if ($_.Exception.InnerException.Response.StatusCode -eq "Moved") { | |
Write-Verbose -Message 'Moved detected.' | |
$Uri = $_.Exception.InnerException.Response.Headers["Location"] | |
}#if_Error_Moved | |
else { | |
Write-Warning -Message 'An Error was encountered resolving a potential shortlink:' | |
Write-Error $_ | |
}#else_Error_Moved | |
}#catch_HttpWebRequest | |
return $Uri | |
#> | |
<#PSScriptInfo | |
.VERSION 1.1 | |
.GUID 52a3719b-0d47-402e-a538-9a1623be12f2 | |
.AUTHOR Lee Holmes | |
.DESCRIPTION Resolve a URI to the URIs it redirects to | |
#> | |
<# | |
.Synopsis | |
Resolve a URI to the URIs it redirects to | |
.EXAMPLE | |
PS> Resolve-Uri https://bitly.is/1g3AhR6 | |
https://bitly.is/1g3AhR6 | |
https://bitly.com/ | |
#> | |
param( | |
## The URI to resolve | |
[Parameter(Mandatory, Position = 0)] | |
$Uri | |
) | |
$ProgressPreference = "Ignore" | |
$ErrorActionPreference = "Stop" | |
while($Uri) | |
{ | |
$Uri | |
$wc = [System.Net.HttpWebRequest]::Create($Uri) | |
$wc.AllowAutoRedirect = $false | |
try | |
{ | |
$response = $wc.GetResponse() | |
if($response.Headers["Location"]) | |
{ | |
$Uri = $response.Headers["Location"] | |
} | |
else | |
{ | |
$Uri = $null | |
} | |
} | |
catch | |
{ | |
if($_.Exception.InnerException.Response.StatusCode -eq "Moved") | |
{ | |
$Uri = $_.Exception.InnerException.Response.Headers["Location"] | |
} | |
else | |
{ | |
throw $_ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment