Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8594157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:08:14+00:00 2026-06-12T00:08:14+00:00

Two weeks ago I found a very interesting article: A HTTP file server in

  • 0

Two weeks ago I found a very interesting article: “A HTTP file server in 130 lines of code”. I translated the original code in Visual basic (I use Visual Basic Net 2005 Express).

Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Web
Public Class HttpFileServer
    Implements IDisposable
    Public rootPath As String
    Private Const bufferSize As Integer = 1024 * 512
    '512KB
    Private ReadOnly http As HttpListener
    Public Sub New(ByVal rootPath As String)
        Me.rootPath = rootPath
        http = New HttpListener()
        http.Prefixes.Add("http://localhost:80/")
        http.Start()
        http.BeginGetContext(requestWait, Nothing)
    End Sub
    Public Sub Dispose() Implements System.IDisposable.Dispose
        http.[Stop]()
    End Sub
    Private Sub requestWait(ByVal ar As IAsyncResult)
        If Not http.IsListening Then
            Return
        End If
        Dim c = http.EndGetContext(ar)
        http.BeginGetContext(requestWait, Nothing)
        Dim url = tuneUrl(c.Request.RawUrl)
        Dim fullPath = IIf(String.IsNullOrEmpty(url), rootPath, Path.Combine(rootPath, url))
        If Directory.Exists(fullPath) Then
            returnDirContents(c, fullPath)
        ElseIf File.Exists(fullPath) Then
            returnFile(c, fullPath)
        Else
            return404(c)
        End If
    End Sub
    Private Sub returnDirContents(ByVal context As HttpListenerContext, ByVal dirPath As String)
        context.Response.ContentType = "text/html"
        context.Response.ContentEncoding = Encoding.UTF8
        Using sw = New StreamWriter(context.Response.OutputStream)
            sw.WriteLine("html")
            sw.WriteLine("head meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8""/head")
            sw.WriteLine("body ul")
            Dim dirs = Directory.GetDirectories(dirPath)
            For Each d As Object In dirs
                Dim link = d.Replace(rootPath, "").Replace("\"c, "/"c)
                sw.WriteLine("<li>&lt;DIR&gt; a href=""" + link + """ " + Path.GetFileName(d) + "/a  /li ")
            Next
            Dim files = Directory.GetFiles(dirPath)
            For Each f As Object In files
                Dim link = f.Replace(rootPath, "").Replace("\"c, "/"c)
                sw.WriteLine(" li <a href=""" + link + """ " + Path.GetFileName(f) + " /a  /li ")
            Next
            sw.WriteLine(" /ul  /body  /html ")
        End Using
        context.Response.OutputStream.Close()
    End Sub
    Private Shared Sub returnFile(ByVal context As HttpListenerContext, ByVal filePath As String)
        context.Response.ContentType = getcontentType(Path.GetExtension(filePath))
        Dim buffer = New Byte(bufferSize - 1) {}
        Using fs = File.OpenRead(filePath)
            context.Response.ContentLength64 = fs.Length
            Dim read As Integer
            While (InlineAssignHelper(read, fs.Read(buffer, 0, buffer.Length))) > 0
                context.Response.OutputStream.Write(buffer, 0, read)
            End While
        End Using
        context.Response.OutputStream.Close()
    End Sub
    Private Shared Sub return404(ByVal context As HttpListenerContext)
        context.Response.StatusCode = 404
        context.Response.Close()
    End Sub
    Private Shared Function tuneUrl(ByVal url As String) As String
        url = url.Replace("/"c, "\"c)
        url = HttpUtility.UrlDecode(url, Encoding.UTF8)
        url = url.Substring(1)
        Return url
    End Function
    Private Shared Function getcontentType(ByVal extension As String) As String
        Select Case extension
            Case ".avi"
                Return "video/x-msvideo"
            Case ".css"
                Return "text/css"
            Case ".doc"
                Return "application/msword"
            Case ".gif"
                Return "image/gif"
            Case ".htm", ".html"
                Return "text/html"
            Case ".jpg", ".jpeg"
                Return "image/jpeg"
            Case ".js"
                Return "application/x-javascript"
            Case ".mp3"
                Return "audio/mpeg"
            Case ".png"
                Return "image/png"
            Case ".pdf"
                Return "application/pdf"
            Case ".ppt"
                Return "application/vnd.ms-powerpoint"
            Case ".zip"
                Return "application/zip"
            Case ".txt"
                Return "text/plain"
            Case Else
                Return "application/octet-stream"
        End Select
    End Function
    Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
        target = value
        Return value
    End Function
End Class

But I got errors:

Error 1 Argument not specified for parameter ‘ar’ of ‘Private Sub requestWait(ar As System.IAsyncResult)’. line 19
Error 2 Argument not specified for parameter ‘ar’ of ‘Private Sub requestWait(ar As System.IAsyncResult)’. line 31

Both errors refer to the expression http.BeginGetContext(requestWait, Nothing). But I don’t know how to handle this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:08:15+00:00Added an answer on June 12, 2026 at 12:08 am

    To compile your code change the lines

    http.BeginGetContext(requestWait, Nothing)
    

    to

    http.BeginGetContext(AddressOf requestWait, Nothing)
    

    You can have a look at AddressOf Operator and this question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Two weeks ago I've read a strange article on the internet (russian), I didn't
I made another question about two weeks ago with having trouble with the Circle
a few weeks ago a co-worker of mine spent about two hours finding out
Until two weeks ago, each time I posted to my company's Facebook Page wall
I released my first Android app two weeks ago (an MMO called Agent Syndicate)
Q: I face the following problem two weeks ago , and i don't know
Two weeks ago I needed a way to communicate a wcf service with a
i just started two weeks ago with ObjectiveC and i'm a complete noob so,
Been banging our heads on this for two weeks now. Any help will be
I have now been working as a web developer for two weeks and have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.