Skip to content

Instantly share code, notes, and snippets.

@nobodyguy
Last active August 29, 2015 13:58
Show Gist options
  • Save nobodyguy/9936036 to your computer and use it in GitHub Desktop.
Save nobodyguy/9936036 to your computer and use it in GitHub Desktop.
PowerShell HTTP server with forced 500 response code
$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