473,320 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

multipart/form-data Post

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 =
"aevom43s98jq30m9w492024234ioafwf02439t0j05wa35w5r ef"

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)

Nov 17 '05 #1
1 4785
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
"LD" <he**@home.com> wrote in message
news:gT43b.275057$YN5.187446@sccrnsc01...
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 =
"aevom43s98jq30m9w492024234ioafwf02439t0j05wa35w5r ef"

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)

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Niels Berkers | last post by:
Hi, i'd like to host my web pages using multiparts to reduce the number of hits on the server. i know this isn't a real PHP subject, but i'll try it anyway. i've been searching the web for...
2
by: Damien | last post by:
Hi to all, After hours of attempts and "googling", I'm still pulling my hair off my head when I try to send multipart html emails. It "works" on PCs with Outlook when I juste send a single...
4
by: Hunter Peress | last post by:
I have been unable to write a script that can do (and receieve) a multipart form upload. Also, it seems that there really are differences between python's implementation and else's. Can someone...
0
by: Travis Pupkin | last post by:
Hi, I have a form that triggers the sending of an e-mail via CDOSYS. I'd like to make this a nice HTML-formatted multipart message, but for some reason the text version is coming through blank....
4
by: John Fereira | last post by:
So, one of the limitations of multipart-form handling is that when an <input type="file" ..> tag is used it will bring up a window which allows a user to select a file for upload but won't allow...
2
by: Der tolle Emil | last post by:
Hi! I wrote a little function to send emails which works quite well. I already managed to send attachments correctly (also more than 1 per email) but I am not able to send a HTML mail containing...
0
by: ptraj | last post by:
Dear ppls, I'm doing a small project to extract all messages from outlook by Visual Basic 6.0 and write them into a file(in MBOX format). While accessing messages i able to get headers, body,...
4
by: DMG | last post by:
I have <identity impersonate = "true" /& <authentication mode="Windows"/in the web.config. This is a 2.0 site. My Goal: I want to simply have the user browse to a file on a mapped drive and get...
2
by: madmak | last post by:
Hi, I am a noob with PHP and need some asistance regarding PHP and lotus notes. I am trying to create a multipart message in PHP to send mail via lotus notes. Here is the code snippet. ...
4
by: ceh | last post by:
Hi, on windows xp I'm using xampp v 1.6.4 I'm trying to send mail. The mail always sends, but the multipart sections are broken. Essentially, I want to send an html email that has a link...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.