阅读提交给HTML表单的帖子数据

时间:2011-08-01 06:13:41

标签: asp.net html vb.net

我在VB.NET中编写了我的HTML网络服务器,我不知道如何让它读取以HTML格式提交的数据值。请帮帮我我的网络服务器只响应HTTP'GET'请求。我的样本鳕鱼低于它正在读取URL而不是表格中包含的数据

    ' 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()`

2 个答案:

答案 0 :(得分:1)

据我所知,对于POST方法,你应该使用Request.Form()

答案 1 :(得分:1)

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