Staypa
Staypa

Reputation: 253

Read Post Data submitted to A HTML Form

I've written my HTML webserver in VB.NET and I don't know how to make it read data values submitted in HTML forms. Please help me My web server only responds to HTTP 'GET' requests. My sample cod is below it is reading the URL rather tan the data contained in the forms`

    ' 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 = "<H2>MONITOR BUSINESS DIRECTORY SEARCH ENGINE SERVER<BR>"
            'sErrorMessage = sErrorMessage & "<BR>"
            'sErrorMessage = sErrorMessage & "<BR><BR>Edit Config.ini and setup the default web path and default page.</H2>"
            'SendHeader(sHttpVersion, "", sErrorMessage.Length, " 404 Not Found")
            'SendToBrowser(sErrorMessage)

            Dim tbReceiver, tbMessage As String
            tbReceiver = "aaaaaaaaaaaaaa"
            tbMessage = "cccccccccccccccc"

            Try
                'mySender = Context.Request.QueryString("sender").ToString
                request.
                url = host + "/api?action=sendmessage&" _
                         & "username=" & HttpUtility.UrlEncode(username) _
                         & "&password=" + HttpUtility.UrlEncode(password) _
                         & "&recipient=" + HttpUtility.UrlEncode(tbReceiver.ToString) _
                         & "&messagetype=SMS:TEXT" _
                         & "&messagedata=" + HttpUtility.UrlEncode(tbMessage.ToString) _
                         & "&originator=" + HttpUtility.UrlEncode(originator) _
                         & "&serviceprovider=" _
                         & "&responseformat=html"


                request = DirectCast(WebRequest.Create(url), HttpWebRequest)

                'response = DirectCast(request.GetResponse(), HttpWebResponse)

                'MessageBox.Show("Response: " & response.StatusDescription)
                MessageBox.Show("Message has been passed to the server for sending")

            Catch ex As Exception
                MessageBox.Show("Error. Server Not running")
            End Try


            mySocket.Close()
            Return
        End If


    End If

    ' get the mime type for the requested file
    Dim sMimeType As String = GetMimeType(sRequestedFile)
    If sMimeType = "" Then
        ' unknown type
        mySocket.Close()
        Return
    End If
    ' Build the complete path to the files
    sPhysicalFilePath = sLocalDir & sDirName & sRequestedFile
    ' Log("Request for file: " & sPhysicalFilePath)
    If Not File.Exists(sPhysicalFilePath) Then
        ' File does not exist
        sErrorMessage = "<H2>404 Error! File Does Not Exist...</H2>"
        SendHeader(sHttpVersion, "", sErrorMessage.Length, " 404 Not Found")
        SendToBrowser(sErrorMessage)
    Else
        ' Create File Stream of filename
        Dim fs As FileStream = New FileStream(sPhysicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)
        ' create reader
        Dim reader As BinaryReader = New BinaryReader(fs)

        ' Create byte array buffer
        Dim bytes As Byte()
        ReDim bytes(CInt(fs.Length))

        ' Read file into byte array
        bytes = reader.ReadBytes(CInt(fs.Length))

        ' Total length of file
        Dim totbytes As Integer = CInt(fs.Length)

        ' close the reader and file stream
        reader.Close()
        fs.Close()

        ' Send HTTP header
        SendHeader(sHttpVersion, sMimeType, totbytes, " 200 OK")

        ' Send File
        SendToBrowser(bytes)
    End If

    ' All done for this connection!
    mySocket.Close()`

Upvotes: 0

Views: 2164

Answers (2)

mirko cro 1234
mirko cro 1234

Reputation: 72

C#

foreach (String s in Request.QueryString) {
    Response.Write(s + " = " + Request.QueryString[s]);
}

VB.net

For Each s As [String] In Request.QueryString
    Response.Write((s & " = ") + Request.QueryString(s)) 
Next

Upvotes: 1

Maysam
Maysam

Reputation: 7367

As I know, for POST method you should use Request.Form()

Upvotes: 1

Related Questions