Lab 6: HTTP Web Server
Introduction:
This lab you will write a simple web server using PowerShell which will send an http response message containing an HTML file to the browser when receiving a http request to get the file.
Lab Procedure:
You will need to open PowerShell as administratorIn the users profile folder under documents create a folder called HTTPIn the HTTP directory create a file called MyWebPage.html . You can copy and paste the code.
<!DOCTYPE html>
<head><title>Lab6</title>
<style type="text/css">
Body {
background-color: gray;
}
H1 {
color: white;
text-align: center;
}
</style>
</head>
<body>
<H1>My First HTTP Response from MyPowerShellSite</H1>
<H1>Send by <type Your Name Here></H1>
</body>
</html>
The web code should be self explanatory, if you do not understand part of the code, see your instructor..
In the same directory we will create a simple HTTP server program. You can use the PowerShell ISE to create the file,BUT YOU CAN NOT RUN THE FILE IN THE ISE. Copy and paste the code intohe ISE. Read the code comments to understand what it is doing.
This simple program will only make 1 connection. The client will send an http request for the file MyWebPage.html. The server will then check its local directory http for the file and send it to the browser. Since the file is a markup file with formatting the browser will display the formatted text. Name the file<LearnName>_Lab6.ps1
$host.UI.RawUI.WindowTitle = "MyPowerShellSite"
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add("http://localhost:8888/")
$listener.Start()
New-PSDrive -Name MyPowerShellSite -PSProvider FileSystem -Root $PWD.Path
$Context = $listener.GetContext()
$URL = $Context.Request.Url.LocalPath
$Content = Get-Content -Encoding Byte -Path "MyPowerShellSite:$URL"
$Context.Response.OutputStream.Write($Content, 0, $Content.Length)
$Context.Response.Close()
Once your server program is completed. Once the PowerShell console and navigate to the HTTP directory.
Note:
if you file does not run, any you get error messages, you will need to close PowerShell console, reopen it and start again.In some systems the newer version of Powershell ISE does not support this -EncodingByte name, so replace it with-AsByteStream [3rd line from the end:$Content = Get-Content -Encoding Byte -Path "MyPowerShellSite:$URL"
Type:. \<LearnName>_Lab6.ps1
Your server should now be running.
Note:
if you get an Execution policy restriction error message then change the execution policy of the power shell by following the steps given below and then run server program again.
Right-click your PowerShell icon and select Run as administrator.Check the current script execution policy by typing Get-ExecutionPolicy on the command line. PowerShell will return a value ofRestricted.To change the script execution policy toUnrestricted, type Set-ExecutionPolicy unrestricted and press EnterAgain type Get-ExecutionPolicy PowerShell should return a value ofUnrestricted.
Open another PowerShell console windowType the following: netstat -ano | select-string “8888”
What is protocol is used to make the connection? ________________________________________
What is the status of the port? ______________________________________________________
Open your browserType the following:http://localhost:8888/MyWebPage.html
If you used a different port number replace the port with the one you identified in the server program. Take a screen shot of the web page and name it LearnName_Lab6.jpeg [0.675 Marks]
Type:netstat -ano | select-string “8888”
_________________________________________________________________________
____________________________________________________________________ _
What is the status of the TCP ports? _______________________________________
Close the browser windowType:netstat -ano | select-string “8888”
What is the status of the port numbers now? ___________________________________
Grading:
Each answer worth 0.2 marks
[Total 1.2 Marks]
LearnName_lab6.jpeg [0.675 Marks]Lab6 HTTP Web Server.docx
Remember replacinglearnnamewith your namefor submission.Submit using the Lab 6 Submission link under MySeneca\Graded Work