473,800 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ToStrin g(Chr(13)) + Convert.ToStrin g(Chr(10))

Dim vbCr As String = Convert.ToStrin g(Chr(13))

Dim vbLf As String = Convert.ToStrin g(Chr(10))

Dim boundary As String =
"aevom43s98jq30 m9w492024234ioa fwf02439t0j05wa 35w5ref"

Dim StrB As New System.Text.Str ingBuilder()

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(Sys tem.Text.Encodi ng.Default.GetS tring(Tem))

Dim myWebClient As New System.Net.WebC lient()

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(webReques t.Create(URL),
HttpWebRequest)

Dim encoding As New System.Text.ASC IIEncoding()

Dim byte1 As Byte() = encoding.GetByt es(body)

webRequest.Meth od = "POST"

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

webRequest.Cont entLength = byte1.Length

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

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

webRequest.Keep Alive = True

Dim newStream As Stream = webRequest.GetR equestStream()

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

newStream.Close ()

Dim webResponse As HttpWebResponse = CType(webReques t.GetResponse() ,
HttpWebResponse )

webRequest.GetR esponse()

Dim sReader As New StreamReader(we bResponse.GetRe sponseStream(), False)

Dim rawHTML As String

rawHTML = sReader.ReadToE nd()

Response.Write( rawHTML)

Nov 17 '05 #1
1 4826
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.co m> wrote in message
news:gT43b.2750 57$YN5.187446@s ccrnsc01...
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.ToStrin g(Chr(13)) + Convert.ToStrin g(Chr(10))
Dim vbCr As String = Convert.ToStrin g(Chr(13))

Dim vbLf As String = Convert.ToStrin g(Chr(10))

Dim boundary As String =
"aevom43s98jq30 m9w492024234ioa fwf02439t0j05wa 35w5ref"

Dim StrB As New System.Text.Str ingBuilder()

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(Sys tem.Text.Encodi ng.Default.GetS tring(Tem))

Dim myWebClient As New System.Net.WebC lient()

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(webReques t.Create(URL),
HttpWebRequest)

Dim encoding As New System.Text.ASC IIEncoding()

Dim byte1 As Byte() = encoding.GetByt es(body)

webRequest.Meth od = "POST"

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

webRequest.Cont entLength = byte1.Length

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

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

webRequest.Keep Alive = True

Dim newStream As Stream = webRequest.GetR equestStream()

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

newStream.Close ()

Dim webResponse As HttpWebResponse = CType(webReques t.GetResponse() ,
HttpWebResponse )

webRequest.GetR esponse()

Dim sReader As New StreamReader(we bResponse.GetRe sponseStream(), False)

Dim rawHTML As String

rawHTML = sReader.ReadToE nd()

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
4475
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 solutions and examples with no succes. does anybody know a good starting point hints / tips are also welcome Regards
2
3206
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 "related" mail: one part for the HTML body, and several for the images. However, the images do not show on a Mac. I also wanted to have an "alternate", plain text message. I've tried the method described by Zend and PHPBuilder, but no luck...
4
2995
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 please prove me wrong with a script that works with itself AND with example 18-2 from php: http://www.php.net/manual/en/features.file-upload.php __________________________________
0
1900
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. Originally I thought I had read that CDO has an automatic text converter that will turn the HTML code into a plain TEXT message, but when that didn't work (and I couldn't find anything further about it), I added a custom text line like this:
4
2824
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 the user to select multiple files. As you may know, the tag produces a text input field with an adjacent button for selecting a file from the local file system. As I would like to be able to allow the user to upload an arbitrary number of files...
2
5046
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 a text only block for non-HTML clients. I will not post the PHP code as I think it is irrelevant, the error lies within the mail header and/or body, so here is the mail I do want to send: FROM: me <foo@bar.com>
0
1443
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, list of attachments. Herer two cases has to be considered. 1. Messages without Attachments. 2. Messages with Attachments. For the first case "Content-Type" will be text/plain or test/html etc..., for 2nd case "Content-Type" will be...
4
3114
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 the UNC for that file and eventually save that unc path to the database as a link. The problem is depending on office we have the same drive letter mapped to different servers. My current test works when I run everything locally and maps...
2
6311
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. <?php ---some code here -- $session_notes = new COM("Lotus.NotesSession"); $session_notes->Initialize("<password>");
4
2887
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 it, like http://www.google.com so the reader can click on the link. When I get the email, the body is empty.
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7580
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6813
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.