Hi guys, i'm really new to programming and VB.NET and i'm attempting to make myself a very simple web server using Httplistener. The code below works perfectly apart from the fact that i'm really not sure how to go about making it serve an image as well as the HTML on request.
I have an image called test.jpg in the same DIR as my application, can anybody point me in the right direction or show me how to make that image load - it shows up as a broken link image when the rest of the page loads.
Thank you...
I have an image called test.jpg in the same DIR as my application, can anybody point me in the right direction or show me how to make that image load - it shows up as a broken link image when the rest of the page loads.
Thank you...
VB.NET:
Imports System.Net.Sockets
Imports System.Text
Imports System.Net
Imports System.Globalization
Module HttpListener
Sub Main()
ProcessRequests()
End Sub
Private Sub ProcessRequests()
If Not System.Net.HttpListener.IsSupported Then
Console.WriteLine( _
"Windows XP SP2, Server 2003, or higher is required to " & _
"use the HttpListener class.")
Exit Sub
End If
' URI prefixes are required,
' Create a listener and add the prefixes.
Dim listener As System.Net.HttpListener = _
New System.Net.HttpListener()
listener.Prefixes.Add("http://*:8001/")
Try
' Start the listener to begin listening for requests.
listener.Start()
Console.WriteLine("Listening...")
' Set the number of requests this application will handle.
Dim numRequestsToBeHandled As Integer = "20"
For i As Integer = 0 To numRequestsToBeHandled
Dim response As HttpListenerResponse = Nothing
Try
' Note: GetContext blocks while waiting for a request.
Dim context As HttpListenerContext = listener.GetContext()
' Create the response.
response = context.Response
Dim responseString As String = _
"<HTML><BODY>The time is currently " & _
DateTime.Now.ToString( _
DateTimeFormatInfo.CurrentInfo) & _
"<img src=test.jpg>" & _
"</BODY></HTML>"
Dim buffer() As Byte = _
System.Text.Encoding.UTF8.GetBytes(responseString)
response.ContentLength64 = buffer.Length
Dim output As System.IO.Stream = response.OutputStream
output.Write(buffer, 0, buffer.Length)
Catch ex As HttpListenerException
Console.WriteLine(ex.Message)
Finally
If response IsNot Nothing Then
response.Close()
End If
End Try
Next
Catch ex As HttpListenerException
Console.WriteLine(ex.Message)
Finally
' Stop listening for requests.
listener.Close()
Console.WriteLine("Done Listening...")
End Try
End Sub
End Module
Last edited: