-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpGet.txt
47 lines (34 loc) · 1.07 KB
/
httpGet.txt
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
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string] $target,
[Parameter(Mandatory=$True,Position=2)]
[string] $verb,
[Parameter(Mandatory=$False,Position=3)]
[string] $content
)
write-host "Http Url: $target"
write-host "Http Verb: $verb"
write-host "Http Content: $content"
$webRequest = [System.Net.WebRequest]::Create($target)
$encodedContent = [System.Text.Encoding]::UTF8.GetBytes($content)
$webRequest.Method = $verb
write-host "UTF8 Encoded Http Content: $content"
if($encodedContent.length -gt 0) {
$webRequest.ContentLength = $encodedContent.length
$requestStream = $webRequest.GetRequestStream()
$requestStream.Write($encodedContent, 0, $encodedContent.length)
$requestStream.Close()
}
[System.Net.WebResponse] $resp = $webRequest.GetResponse();
if($resp -ne $null)
{
$rs = $resp.GetResponseStream();
[System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;
[string] $results = $sr.ReadToEnd();
return $results
}
else
{
exit ''
}