multipart/form-data Post

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

LD

Hi,

I'm pulling my hair out!!

My problem is,

I need to automatically upload a zip file along with 3 other pieces of text
data to a web server and wait for it's xml response. Basically a
multipart/form-data post. I have tried it using a regular html form and it
works but that is no good because it leaves you at the other server where
you posted the data and I need to process the xml response and redirect. I
have tried the following code below and I either get that the post is
malformed or the connection was terminated. Is there a better way to do
this? I really appreciate any insite into this.

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 =
"aevom43s98jq30m9w492024234ioafwf02439t0j05wa35w5ref"

Dim StrB As New System.Text.StringBuilder()

Dim Tem() As Byte

Dim st As FileStream = File.OpenRead("E:\data.zip")

ReDim Tem(CInt(2 ^ 15))

Do While st.Position < st.Length

st.Read(Tem, 0, Tem.Length - 1)

Loop

st.Close()

StrB.Append(System.Text.Encoding.Default.GetString(Tem))

Dim myWebClient As New System.Net.WebClient()

Dim URL As String

Dim body As String

body = "Data found after header end:" & vbCrLf & vbCrLf & _

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 & _

"""AUI""" & vbCrLf

body = body & boundary & vbCrLf & _

"Content-Disposition: " & "form-data; name=""data""" & ";
filename=""data.zip""" & vbCrLf & vbCrLf & _

"Content-Type: application/x-zip-compressed" & vbCrLf & vbCrLf & _

StrB.ToString() & vbCrLf & boundary & "--"

URL = "server I need to send to."

Dim webRequest As HttpWebRequest = CType(webRequest.Create(URL),
HttpWebRequest)

Dim encoding As New System.Text.ASCIIEncoding()

Dim byte1 As Byte() = encoding.GetBytes(body)

webRequest.Method = "POST"

webRequest.ContentType = "multipart/form-data; boundary=" & boundary

webRequest.ContentLength = byte1.Length

webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;
Q312461; .NET CLR 1.0.3705)"

webRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/vnd.ms-excel, application/msword, application/x-shockwave-flash,
*/*"

webRequest.KeepAlive = True

Dim newStream As Stream = webRequest.GetRequestStream()

newStream.Write(byte1, 0, byte1.Length)

newStream.Close()

Dim webResponse As HttpWebResponse = CType(webRequest.GetResponse(),
HttpWebResponse)

webRequest.GetResponse()

Dim sReader As New StreamReader(webResponse.GetResponseStream(), False)

Dim rawHTML As String

rawHTML = sReader.ReadToEnd()

Response.Write(rawHTML)
 
Hello

I think your problem is that you convert the binary content to a string,
concatenate it to the rest of the data and convert back to byte[] array. May
be the problem is that a null byte in the binary file is interpreted as a
string null terminator. So you get an incorrect string. Better convert the
other strings to byte arrays and then write the byte arrays in correct order
to the webrequest object.

Hope this info helps

Best Regards
 
Back
Top