-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-Header.ps1
54 lines (43 loc) · 1.25 KB
/
Get-Header.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<#
.SYNOPSIS
Get http header of a server specified by URL
.DESCRIPTION
Utilize HttpWebRequest and HttpWebResponse to retrieve header
.PARAMETER URL
URI
.EXAMPLE
$ Get-Header.ps1 http://rtur.net
.NOTES
- first write, 06-25-2011
tag: windows-only
#>
param([string]$Url)
if (! $Url) {
$Url = "https://note.iqubit.xyz"
echo "Defaulting to target host: $Url"
}
if (! ($Url.StartsWith("https://")) -and ! ($Url.StartsWith("http://"))) {
$Url = "http://" + $Url
}
$WebRequestObject = [System.Net.HttpWebRequest] [System.Net.WebRequest]::Create($Url)
try{
$ResponseObject = [System.Net.HttpWebResponse] $WebRequestObject.GetResponse()
}
catch [Net.WebException] {
echo $_.Exception.ToString()
break
}
echo "Header for $Url`n--------------------------------------"
foreach ($HeaderKey in $ResponseObject.Headers) {
# "$HeaderKey"
$HeaderStr = $ResponseObject.Headers[$HeaderKey]
if ($HeaderStr) {
# Doesn't display big cookie
if ($HeaderKey.Equals("Set-Cookie") -and $HeaderStr.Length -gt 180) {
$HeaderStr = $HeaderStr.Substring(0, 180) + " ..."
}
echo "$HeaderKey`: $HeaderStr"
}
}
echo "--------------------------------------`nClosing connection."
$ResponseObject.Close()