Imports System.Net Imports System.Text Imports System.IO Public Class Form1 Dim httpTrd As New Threading.Thread(AddressOf httpProc) Dim Buf As String = "" Private Sub httpProc() Dim Sv As New HttpListener '建立一個 HTTP 監聽器 Sv.Prefixes.Add("http://*:8000/") '指定以通訊埠 8000 收訊 Sv.Start() '啟動監聽到達本機通訊埠 8000 的 HTTP 訊息 While True '無窮迴圈,只有程式結束時會停止 Dim Ct As HttpListenerContext = Sv.GetContext '自監聽器取得客戶呼叫內容 Dim Rq As HttpListenerRequest = Ct.Request '取得 Request 物件 Dim Rp As HttpListenerResponse = Ct.Response '取得 Response 物件 Buf = Rq.RawUrl '收到的客戶訊息 '在此依據收到的客戶訊息產生回覆,包括:計算或查詢資料庫等等 Dim b() As Byte = Encoding.Default.GetBytes("Got: " + Buf) '回覆客戶的文字訊息轉為二進位碼 Rp.ContentLength64 = b.Length '二進位碼的長度 Dim out As Stream = Rp.OutputStream '建立回傳客戶的資料流 out.Write(b, 0, b.Length) '回傳 out.Close() End While Sv.Stop() End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Label1.Text = Uri.UnescapeDataString(Buf) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load httpTrd.Start() End Sub End Class