Last active
May 31, 2016 23:27
-
-
Save rms1000watt/ad29c95049272609b4cfc945d7e88755 to your computer and use it in GitHub Desktop.
Send Synchronous HTTP Request Powershell 2
This file contains hidden or 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
function sendHTTP | |
{ | |
param( $uri, $message ) | |
$postData = "{" | |
$postData += "`"message`":`""+$message+"`"," | |
$postData += "`"name`":`"ryan`"" | |
$postData += "}" | |
$request = [System.Net.WebRequest]::Create($uri) | |
$request.Method = "POST" | |
$request.Timeout = 5000 | |
try | |
{ | |
$requestStream = $request.GetRequestStream() | |
$streamWriter = New-Object System.IO.StreamWriter($requestStream) | |
$streamWriter.Write($postData) | |
} | |
catch | |
{ | |
Write-Host "HTTP Request Failed (1).." | |
} | |
finally | |
{ | |
if ($null -ne $streamWriter) { $streamWriter.Dispose() } | |
if ($null -ne $requestStream) { $requestStream.Dispose() } | |
} | |
try | |
{ | |
$res = $request.GetResponse() | |
} | |
catch | |
{ | |
Write-Host "HTTP Request Failed (2).." | |
} | |
finally | |
{ | |
if ($null -ne $res) { $res.Close() } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment