How can my server program read the variables sent

2019-08-02 08:48发布

问题:

I have written server code in vb.Net. I want it to read to variables sent by another server's HTTP 'GET' request. For example the first server will send this URL http://localhost/sms/incoming.php?sender=$originator&receiver=$recipient&msgdata=$messagedata&recvtime=$receivedtime&msgid=$messageid where the values to be used by my server are sender, receiver, msgdata, recvtime and msgid I have written my code but it only reads the address sent by the http server and locates for the file in the server's root directory. I want the server to be reading the variables sent by the other server using the HTTP 'GET' request. My code is shown below

' 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:

You can get them from the Context.Request.QueryString object:

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

This assumes the code you are running on your server is running under IIS. I notice the call is to a .PHP page and wonder how you are actually getting the url to the server running the VB code.

If this is simply processing the URL as a string, then the following should handle it for you:

        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


标签: html vb.net http