Monday, January 20, 2025

simple webserver for testing on Windows using Powershell

 This needs to be run in a Powershell run as administrator on my machine. Via https://woshub.com/simple-http-webserver-powershell/


$httpListener = New-Object System.Net.HttpListener

$httpListener.Prefixes.Add("http://+:9090/")

$httpListener.Start()


write-host "Press any key to stop the HTTP listener after next request"

while (!([console]::KeyAvailable)) {

$context = $httpListener.GetContext()

$context.Response.StatusCode = 200

$context.Response.ContentType = 'text/HTML'

$WebContent = Get-Content -Path "D:\Downloads\new.html" -Encoding UTF8

$EncodingWebContent = [Text.Encoding]::UTF8.GetBytes($WebContent)

$context.Response.OutputStream.Write($EncodingWebContent , 0, $EncodingWebContent.Length)

$context.Response.Close()

Write-Output "" # Newline

}

$httpListener.Close()

No comments:

Post a Comment