Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.Net中的HTTP文件服务器(130行代码)_Vb.net_Http - Fatal编程技术网

VB.Net中的HTTP文件服务器(130行代码)

VB.Net中的HTTP文件服务器(130行代码),vb.net,http,Vb.net,Http,两周前,我发现了一篇非常有趣的文章:。我用visualbasic翻译了原始代码(我使用visualbasicnet2005express) 导入系统 导入System.IO 导入系统.Net 导入系统文本 导入系统.Web 公共类HttpFileServer 实现IDisposable 作为字符串的公共根路径 Private Const bufferSize为整数=1024*512 512KB 专用只读http作为HttpListener Public Sub New(ByVal rootPat

两周前,我发现了一篇非常有趣的文章:。我用visualbasic翻译了原始代码(我使用visualbasicnet2005express)

导入系统
导入System.IO
导入系统.Net
导入系统文本
导入系统.Web
公共类HttpFileServer
实现IDisposable
作为字符串的公共根路径
Private Const bufferSize为整数=1024*512
512KB
专用只读http作为HttpListener
Public Sub New(ByVal rootPath作为字符串)
Me.rootPath=rootPath
http=newhttplistener()
http.Prefixes.Add(“http://localhost:80/")
http.Start()
http.BeginGetContext(requestWait,Nothing)
端接头
Public Sub Dispose()实现System.IDisposable.Dispose
http.[Stop]()
端接头
私有子请求等待(ByVal ar作为IAsyncResult)
如果不是http.IsListening,则
返回
如果结束
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))
如果目录.存在(完整路径),则
返回目录内容(c,完整路径)
ElseIf File.Exists(完整路径)则
返回文件(c,完整路径)
其他的
返回404(c)
如果结束
端接头
私有子returnDirContents(ByVal上下文作为HttpListenerContext,ByVal dirPath作为字符串)
context.Response.ContentType=“text/html”
context.Response.ContentEncoding=Encoding.UTF8
使用sw=newstreamwriter(context.Response.OutputStream)
sw.WriteLine(“html”)
sw.WriteLine(“head meta http equiv=”“Content Type”“Content=”“text/html;charset=utf-8”“/head”)
西南书写线(“主体ul”)
Dim dirs=Directory.GetDirectories(dirPath)
对于每个d作为DIR中的对象
Dim link=d.Replace(rootPath,“”)。Replace(“\”c,“/”c)
sw.WriteLine(“
  • DIR a href=”“+link+”“+Path.GetFileName(d)+/a/li”) 下一个 Dim files=目录.GetFiles(dirPath) 对于文件中的每个f As对象 Dim link=f.Replace(rootPath,“”)。Replace(“\”c,“/”c)
    WriteLine(“li要编译代码,请更改行

    http.BeginGetContext(requestWait, Nothing)
    


    您可以查看和。

    非常感谢您提供的解决方案和链接。我编译了代码,服务器正在运行。可能需要一些改进,但一开始非常好。问题已经解决!您如何使用此服务器?
    http.BeginGetContext(requestWait, Nothing)
    
    http.BeginGetContext(AddressOf requestWait, Nothing)