我的服务器程序如何读取VB.NET中使用HTTP'GET'请求方法发送的变量?

时间:2011-07-31 14:32:36

标签: html vb.net http

我在vb.Net中编写过服务器代码。我希望它读取由另一个服务器的HTTP'GET'请求发送的变量。例如,第一台服务器将发送此URL http://localhost/sms/incoming.php?sender=$originator&receiver=$recipient&msgdata=$messagedata&recvtime=$receivedtime&msgid=$messageid我的服务器使用的值是 sender,receiver,msgdata,recvtime msgid 我已编写了我的代码,但它只读取发送的地址通过http服务器并找到服务器根目录中的文件。我希望服务器使用HTTP'GET'请求读取其他服务器发送的变量。我的代码如下所示

' the web server only accepts get requests.
    If Mid(LCase(sbuffer), 1, 3) <> "get" Then
        'if not GET request then close socket and exit
        mySocket.Close()
        Return
    End If

    ' Extract path and filename from request
    sRequest = sbuffer.Substring(0, iStartPos - 1)
    sRequest.Replace("\\", "/")
    If ((sRequest.IndexOf(".") < 1) AndAlso (Not sRequest.EndsWith("/"))) Then
        sRequest = sRequest & "/"
    End If

    iStartPos = sRequest.LastIndexOf("/") + 1

    ' Get the filename
    sRequestedFile = sRequest.Substring(iStartPos)

    ' Get the relative path
    sDirName = sRequest.Substring(sRequest.IndexOf("/"), sRequest.LastIndexOf("/") - 3)

    ' Web server root path
    sLocalDir = sMyWebServerRoot

    ' if no filename specified
    ' look for default file
    If (sRequestedFile.Length = 0) Then
        sRequestedFile = _DefaultPage
        sPhysicalFilePath = sLocalDir & sDirName & sRequestedFile

        ' if no default file and no directory requested
        ' then show welcome page
        If Not File.Exists(sPhysicalFilePath) AndAlso (sDirName = "" OrElse sDirName = "/") Then

            sErrorMessage = sErrorMessage & "<BR><BR>No root directory found. Set up the root directory in the configuration file.</H2>"
            SendHeader(sHttpVersion, "", sErrorMessage.Length, " 404 Not Found")
            SendToBrowser(sErrorMessage)
            mySocket.Close()
            Return
        End If


    End If

1 个答案:

答案 0 :(得分:1)

您可以从Context.Request.QueryString对象中获取它们:

mySender = Context.Request.QueryString("sender").ToString

这假设您在服务器上运行的代码在IIS下运行。我注意到调用是一个.PHP页面,并想知道你是如何实际获取运行VB代码的服务器的URL。

如果这只是将URL作为字符串处理,那么以下内容应该为您处理:

        Dim tempArray As String()
    Dim tempValuePairs As String()
    Dim tempPair As String()
    Dim tempValue As String
    Dim tempString As String

    tempArray = sRequest.Split(CChar("?"))
    If tempArray.Length <> 2 Then
        'url not valid
        Return
    End If
    tempValuePairs = tempArray(1).Split(CChar("&"))

    For Each tempString In tempValuePairs
        tempPair = tempString.Split(CChar("="))
        tempValue = tempPair(1)

        'check for pair name, tempvalue will contain data
        Select Case tempPair(0).ToLower
            Case "sender"
                mySender = tempValue
            Case "receiver"
            Case "msgdata"
            Case "recvtime"
            Case "msgid"
            Case Else
        End Select
    Next