PDA

Click to See Complete Forum and Search --> : HTTP multipart/form-data form data Post


BenoitGirard
May 29th, 2007, 09:05 PM
Edit: I found the solution after talking a one hour break, thanks for looking here anyways! If an admin see this, you may delete this post!

I'm currently working as a QA Tester and I need to automate the web testing. It's just repetive work so I found the following code to post normal data "application/x-www-form-urlencoded" type data:

Private Sub cmdSubmit_Click()
edtPostData.Text = "" ' Initialize an edit box for testing
ReDim aByte(0) As Byte ' Array of bytes to hold data to post

' Extract the URL encoded data from the UI,
' and pack it into an array of bytes
cPostData = startStr()
PackBytes aByte(), cPostData

' For testing, rebuild the POST data and stuff
' it into an edit box
For i = LBound(aByte) To UBound(aByte)
edtPostData = edtPostData + Chr(aByte(i))
Next

Dim vPost As Variant
vPost = aByte ' Assign the byte array to a VARIANT
Dim vFlags As Variant
Dim vTarget As Variant
Dim vHeaders As Variant
vHeaders = _
"Content-Type: application/x-www-form-urlencoded" _
+ Chr(10) + Chr(13)

' You're done. Now call Navigate
' Note: modify path to server as appropriate
g_oIE.Navigate "http://<server>", _
vFlags, vTarget, vPost, vHeaders



End Sub
' Utility function to pack the data to post into an array of bytes
Private Sub PackBytes(ByteArray() As Byte, ByVal PostData As String)
iNewBytes = Len(PostData) - 1 ' Get rid of the null termination
If iNewBytes < 0 Then
Exit Sub
End If
ReDim ByteArray(iNewBytes)
For i = 0 To iNewBytes
ch = Mid(PostData, i + 1, 1)
If ch = Space(1) Then
ch = "+"
End If
ByteArray(i) = Asc(ch)
Next
End Sub

It work fine and is about the equivilent navigating to "http://server/app?blah=fo&param=value". However more complicated forms use the following "multipart/form-data" encoding and I can't find any way to change the code to automate those. Is there a way I could change the code, or just navigate to a certain url that would have the same effect?

Here the modification I made so far but it's not working:

Private Sub Command1_Click()
edtPostData.Text = "" ' Initialize an edit box for testing
ReDim aByte(0) As Byte ' Array of bytes to hold data to post

' Extract the URL encoded data from the UI,
' and pack it into an array of bytes
cPostData = Text1
PackBytes aByte(), cPostData

' For testing, rebuild the POST data and stuff
' it into an edit box
For i = LBound(aByte) To UBound(aByte)
edtPostData = edtPostData + Chr(aByte(i))
Next

Dim vPost As Variant
vPost = aByte ' Assign the byte array to a VARIANT
Dim vFlags As Variant
Dim vTarget As Variant
Dim vHeaders As Variant
vHeaders = _
encTypeHeader() _
+ Chr(10) + Chr(13)

' You're done. Now call Navigate
' Note: modify path to server as appropriate
g_oIE.Navigate "http://<server>?func=ll&objAction=copy&ObjId=2112", _
vFlags, vTarget, vPost, vHeaders
End Sub

Where Text1 holds a cut in paste of a valid Post Data String.

The code results in the web server telling me "Error parsing the GET/POST data stream.". Just a guest but is it possible that the new line character needs to be changed for another character before making the request? Or any other ideas on what I might be doing wrong?