Last active
August 29, 2015 13:58
-
-
Save nobodyguy/9936036 to your computer and use it in GitHub Desktop.
PowerShell HTTP server with forced 500 response code
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
$listener = New-Object System.Net.HttpListener | |
$listener.Prefixes.Add('http://+:8000/') # Must exactly match the netsh command above | |
$listener.Start() | |
Write-Host "Listening..." | |
while ($listener.IsListening) { | |
$context = $listener.GetContext() # blocks until request is received | |
$request = $context.Request | |
$response = $context.Response | |
# This will terminate the script. Remove from production! | |
if ($request.Url -match '/end$') { break } | |
[byte[]] $buffer = [System.Text.Encoding]::UTF8.GetBytes($message) | |
$response.ContentLength64 = $buffer.length | |
$response.StatusCode = 500 | |
$output = $response.OutputStream | |
$output.Write($buffer, 0, $buffer.length) | |
$output.Close() | |
} | |
$listener.Stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment