HTTP Form Post

  • Thread starter Thread starter LD
  • Start date Start date
L

LD

Hi,

I am trying to upload a zip file and other form data elements to a server
and get a response. The server keeps responding with Malformed Multipart
Post. Can
anyone see what might be wrong here? Any feedback much appreciated.

Dim vbCrLf As String = Convert.ToString(Chr(13)) + Convert.ToString(Chr(10))
Dim vbCr As String = Convert.ToString(Chr(13))
Dim vbLf As String = Convert.ToString(Chr(10))
Dim boundary As String = "-----------------------------7d31151970286"

Dim myWebClient As New System.Net.WebClient()
Dim URL As String
Dim body As String
Dim body2 As String

'============READ FILE CONTENTS INTO BYTE ARRAY============
Dim Tem() As Byte
Dim st As FileStream = File.OpenRead("data.zip")
ReDim Tem(st.Length)
Do While st.Position < st.Length
st.Read(Tem, 0, Tem.Length - 1)
Loop
st.Close()

'==================SET HEADERS======================
myWebClient.Headers.Add("Accept", "image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, application/vnd.ms-excel, application/msword,
application/x-shockwave-flash, */*")
myWebClient.Headers.Add("Accept-Language", "en-us")
myWebClient.Headers.Add("Content-Type", "multipart/form-data; boundary=" +
boundary)
myWebClient.Headers.Add("Accept-Encoding", "gzip, deflate")
myWebClient.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.1; Q312461; .NET CLR 1.0.3705)")
myWebClient.Headers.Add("Cache-Control", "no-cache")

'==================CREATE BODY======================
body = boundary & vbCrLf & _
"Content-Disposition: " & "form-data; name=""username""" & vbCrLf & vbCrLf &
_
"username" & vbCrLf

body = body & boundary & vbCrLf & _
"Content-Disposition: " & "form-data; name=""password""" & vbCrLf & vbCrLf &
_
"password" & vbCrLf

body = body & boundary & vbCrLf & _
"Content-Disposition: " & "form-data; name=""client""" & vbCrLf & vbCrLf & _
"client" & vbCrLf

body = body & boundary & vbCrLf & _
"Content-Disposition: " & "form-data; name=""data""" & ";
filename=""data.zip""" & vbCrLf & _
"Content-Type: application/x-zip-compressed" & vbCrLf & vbCrLf

body2 = vbCrLf & boundary & "--"

Dim byte1 As Byte()
Dim byte2 As Byte()
byte1 = System.Text.Encoding.Default.GetBytes(body)
byte2 = System.Text.Encoding.Default.GetBytes(body2)

'===========JOIN BODY AND FILE CONENTS============
Dim pobjCombinedArrays = New ArrayList(byte1.Length + Tem.Length +
byte2.Length)

'Append the arrays here.
pobjCombinedArrays.AddRange(byte1)
pobjCombinedArrays.AddRange(Tem)
pobjCombinedArrays.AddRange(byte2)

Dim pbytCombinedArrays(pobjCombinedArrays.Count) As Byte
pobjCombinedArrays.CopyTo(pbytCombinedArrays)

'===========POST DATA AND GET RESPONSE============
Dim bResponse As Byte() = myWebClient.UploadData(URL, "POST",
pobjCombinedArrays)

Response.Write(System.Text.Encoding.ASCII.GetString(bResponse))
 
Hi,
Just a quick look and maybe I am wrong,
Dim st As FileStream = File.OpenRead("data.zip")
Are you sure this instruction finds the right text file in the zip?

Maybe you know, but if not, there is a webclient.uploadfile methode to.
I hope this helps a little bit.
Cor
 
LD said:
Dim vbCrLf As String = Convert.ToString(Chr(13)) + Convert.ToString(Chr(10))
Dim vbCr As String = Convert.ToString(Chr(13))
Dim vbLf As String = Convert.ToString(Chr(10))

The constants above are already defined in the
'Microsoft.VisualBasic.Constants' class.
 
Back
Top