Burada temel bir web sunucusu oluşturmak için .net nesnelerinden System.Net.HttpListener kullanıyorum. Bu nesne, tarayıcıda web sayfaları kodunu gösterme yeteneğine sahiptir.

Öncelikle html kodumuzu yazalım.
$url = ‘http://localhost:8080/' $htmlCode = @” <!DOCTYPE html> <html> <body> <h1>PowerShell Web Server</h1> <p>Example Web Server with Http Listener</p></br> <p>Resul COŞKUN</p> <p>resulcoskun.com.tr</p> </body> </html> “@
$htmlListener = New-Object System.Net.HttpListener $htmlListener.Prefixes.Add($url) $htmlListener.Start() $httpContext = $htmlListener.GetContext() $httpResponse = $httpContext.Response
Ardından gelen http isteklerinde html kodumuzu UTF-8 formatına dönüştürerek yayınlıyoruz. Sürekli hizmet veren web sayfaları için burada while döngüsü kullanabilirsiniz.
$buffer = [Text.Encoding]::UTF8.GetBytes($htmlCode) $httpResponse.ContentLength64 = $buffer.length $httpResponse.OutputStream.Write($buffer, 0, $buffer.length)
Son olarak servisi durdurmak için aşağıdaki komutu kullanabilirsiniz.
$httpResponse.Close() $htmlListener.Stop()

Özellikle HTML içeriğini UTF-8’e dönüştürüp ContentLength64 ayarlamanız, tarayıcıda karakter sorunu yaşamamanızı sağlıyor. Ayrıca while döngüsü ekleyerek sunucuyu sürekli açık tutmak, test ortamları için harika bir yöntem. Bu örnek, hızlı bir prototip oluşturmak isteyenler için çok pratik bir referans.