Google Answers Logo
View Question
 
Q: Read HTTP body content in ASP ( No Answer,   4 Comments )
Question  
Subject: Read HTTP body content in ASP
Category: Computers > Internet
Asked by: fireduck-ga
List Price: $3.00
Posted: 30 Jun 2004 15:50 PDT
Expires: 30 Jul 2004 15:50 PDT
Question ID: 368330
Hello,

I would like to know how to read the raw HTTP body content in ASP from
a POST action? I don't need to handle binary data. I assume it is text
data.

Thks for your help
Answer  
There is no answer at this time.

Comments  
Subject: Re: Read HTTP body content in ASP
From: higs-ga on 02 Jul 2004 14:59 PDT
 
if posted value is binary, request value is binary.
for example, upload file

request.form("feildName")
do you know this code?
Subject: Re: Read HTTP body content in ASP
From: fireduck-ga on 03 Jul 2004 15:42 PDT
 
I know that code. But I want to see the whole "RAW" body content of
POST action. You may assume I don't know what form variables or how
many form variables used in POST submission. I want to be able to
display the WHOLE submission content in ASP.

Is it possible?

thks
Subject: Re: Read HTTP body content in ASP
From: jsgreenwood-ga on 04 Jul 2004 17:31 PDT
 
Hi there.  Yes, this can be done.  The problem is that the POST always
returns binary data, which isn't too great for working with in ASP. 
However, this can be fixed easily.  Below is a web page that'll
retrieve the POST as binary, then convert it to a string
representation in ASP and write it out to show what's happened:

8<- - - - - - - - 8<- - - - - - - - 

<html>
  <body>
<%
  Sub Main()
    Dim varRequest 'As Variant
    Dim lngLength 'As Long
    
    lngLength = Request.TotalBytes
    
    varRequest = Request.BinaryRead(lngLength)
    'varRequest is now a binary object
    
    varRequest = BStr2UStr(varRequest)
    'varRequest is now a string
    
    If (varRequest <> "") Then
      Response.Write "<b>Data posted was:</b><br /><code>" &
varRequest & "</code><hr />" & vbCrLf
    End If
  End Sub


  Private Function BStr2UStr(BStr)
    'Convert a Byte string to a Unicode string
    On Error Resume Next
    Dim lngCount 'As Long
    
    BStr2UStr = ""
    For lngCount = 1 To LenB(BStr)
      BStr2UStr = BStr2UStr & Chr(AscB(MidB(BStr, lngCount, 1)))
    Next
  End Function

  
  Main
%>

    <form method="post">
      <input type="text" name="txtTextBox" value="My text box value" />
      <br />
      <textarea name="txtTextArea" cols="40" rows="5">Lorem ipsum
dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.</textarea>
      <br />
      <input type="submit" value="submit" />
    </form>
  </body>
</html>


8<- - - - - - - - 8<- - - - - - - - 

What you'll probably find is you want to decode the text posted now
(get rid of all the "+" and "%" values between words, etc.  Here's a
function to do that properly, too:

Private Function URLDecode(ByVal Expression As String) As String
    '== The inverse of the standard ASP function URLEncode()
    Dim strSource       'As String
    Dim strTemp         'As String
    Dim strResult       'As String
    Dim lngCount        'As Long
    Dim lngMax          'As Long

    strSource = Replace(Expression, "+", " ")
    lngMax = Len(strSource)
    For lngCount = 1 To lngMax
        strTemp = Mid(strSource, lngCount, 1)
        If strTemp = "%" Then
            If (lngCount + 2) < Len(strSource) Then
                strResult = strResult & Chr(CInt("&H" & Mid(strSource,
lngCount + 1, 2)))
                lngCount = lngCount + 2
            End If
        Else
            strResult = strResult & strTemp
        End If
    Next

    URLDecode = strResult
End Function
Subject: Re: Read HTTP body content in ASP
From: jsgreenwood-ga on 05 Jul 2004 03:50 PDT
 
One thing I forgot to add in the respnose above was that if you don't
know what's been posted to the page, you can iterate through both the
keys and values really easily, as shown in the following page.

8<- - - - - - - - 8<- - - - - - - - 

<%  Option Explicit %>
<html><body>
<%
  Dim intCount 'As Integer
  Dim strKey 'As String
  Response.Write "<table>" & vbCrLf
  Response.Write "<tr><th>Item</th><th>Value</th></tr>" & vbCrLf
  For intCount=1 To Request.Form.Count
    strKey = Request.Form.Key(intCount)
    Response.Write "<tr><td>" & strKey & "</td><td>" &
Request.Form.Item(strKey) & "</td></tr>" & vbCrLf
  Next
  Response.Write "</table>" & vbCrLf
%>
</body></html>

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy