获得请求 URL

Get Requested URL

我一直在尝试将任何请求的 URL 读取到我的本地主机。我认为最简单的方法是使用 TCPListener。以下是我到目前为止构建的内容,但我不确定如何读取作为字符串请求的 URL。我实际上想获取第一个传入 URL,解析出我需要的数据,然后关闭 TCPlistener。知道如何获得 URL 吗?

Dim TClient As New TcpListener(Net.IPAddress.Parse("127.0.0.1"), 80)
TClient.Start()
Dim gotIt As Boolean = False
Do While gotIt = False
    Dim x = TClient.AcceptTcpClient()
    Console.WriteLine(x)
    TClient.Stop()
Loop

使用 MSDN 中的示例,我得出以下结论:

    Dim server As TcpListener = Nothing

    Dim port As Int32 = 80
    Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
    server = New TcpListener(localAddr, port)
    server.Start()
    Dim bytes(1024) As Byte
    Dim data As String = Nothing

    Dim client As TcpClient = server.AcceptTcpClient
    data = Nothing
    Dim stream As NetworkStream = client.GetStream
    Dim i As Int32
    i = stream.Read(bytes, 0, bytes.Length)

    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)

    client.Close()
    server.Stop()

data变量里有我要找的信息,解析出来就可以了

谢谢@CoderDennis

您要查找的是 Referer header。

String requestedUrl = request.getHeader("Referer");

请注意 Referer 区分大小写。

100% 工作!