By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,530 Members | 2,181 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,530 IT Pros & Developers. It's quick & easy.

Issue with HttpWebRequest & multipart/form-data

P: n/a
Du
I'm trying to automate the upload process to yousendit.com, but the file
size doesn't add up and yousendit.com keep rejecting my upload (it accepts
the upload until the very end)

I don't know what i'm missing, I use Fiddler and I got my header and body to
a very close match, but the content-length is slightly off and I got no clue
why

Sorry for the lengthy post, but i have no other way of showing my work

Can someone please explain the difference in content-length, I can't seem to
work it out. I been searching on google and the news group, but i got
nothing.
Thanks for any input


// =======My Request Header==========

Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword,
application/x-shockwave-flash, */*
Referer: http://s12.yousendit.com/
Content-Type: multipart/form-data;
boundary=---------------------------7d5158213e047e
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.1.4322)
Pragma: no-cache
Accept-Languag: en-us
Content-Length: 1766 <--- notice this is only 1766 byte, but the IE
value is 2026
Expect: 100-continue
Proxy-Connection: Keep-Alive
Host: s12.yousendit.com

// ============ My Request Body ======================

---------------------------7d5158213e047e
Content-Disposition: form-data; name="__VIEWSTATE"

dDwtMTgyNzA1MTg5OztsPFVwbG9hZEZpbGU7Pj6IqrAZhsvVRc 9Qe1y6cNu6G3V3nQ==
---------------------------7d5158213e047e
Content-Disposition: form-data; name="ToEMail"

mi*******@yahoo.com
---------------------------7d5158213e047e
Content-Disposition: form-data; name="LoadFileName";
filename="D:\Desktop\encoder_check.zip"
Content-Type: application/octet-stream

PK



Same file but using internet explorer to upload
// ========== request header ==========
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword,
application/x-shockwave-flash, */*
Accept-Language: en-us
Content-Type: multipart/form-data;
boundary=---------------------------7d533333730406
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.1.4322)
Host: s12.yousendit.com
Content-Length: 2026
Proxy-Connection: Keep-Alive
Pragma: no-cache
// =========== request body ===============
-----------------------------7d533333730406
Content-Disposition: form-data; name="__VIEWSTATE"

dDwtMTgyNzA1MTg5OztsPFVwbG9hZEZpbGU7Pj6IqrAZhsvVRc 9Qe1y6cNu6G3V3nQ==
-----------------------------7d533333730406
Content-Disposition: form-data; name="ToEMail"

mi*******@yahoo.com
-----------------------------7d533333730406
Content-Disposition: form-data; name="LoadFileName";
filename="D:\Desktop\encoder_check.zip"
Content-Type: application/x-zip-compressed

PK

// ============ My Code ==============
string url = @"http://s12.yousendit.com/default.uplx";

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

req.Method = "POST";

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

req.Referer = @"http://s12.yousendit.com/";

req.ContentType = @"multipart/form-data;
boundary=---------------------------7d5158213e047e";

req.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;
..NET CLR 1.1.4322)";

req.KeepAlive = true;

req.Headers.Add("Pragma", "no-cache");

req.Headers.Add("Accept-Languag", "en-us");

req.Timeout = Timeout.Infinite;
string payload =
"---------------------------7d5158213e047e\r\nContent-Disposition:
form-data;
name=\"__VIEWSTATE\"\r\n\r\ndDwtMTgyNzA1MTg5OztsPF VwbG9hZEZpbGU7Pj6IqrAZhsvVRc9Qe1y6cNu6G3V3nQ==\r\n---------------------------7d5158213e047e\r\nContent-Disposition:
form-data; name=\"ToEMail\"\r\n\r\n" + txtEmail.Text +
"\r\n---------------------------7d5158213e047e\r\nContent-Disposition:
form-data; name=\"LoadFileName\"; filename=\"" + filename +
"\"\r\nContent-Type: " + getMimeType(filename) + "\r\n\r\n";

FileInfo file = new FileInfo(filename);

int filesize = Convert.ToInt32(file.Length);
req.ContentLength = payload.Length + file.Length;

// ==================== Start writing ====================

Stream conn = req.GetRequestStream();
byte[] body_header = System.Text.Encoding.ASCII.GetBytes(payload);

conn.Write(body_header, 0, body_header.Length);

FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);

byte[] file_content = new byte[filesize];

int len = fs.Read(file_content, 0, filesize);

conn.Write(file_content, 0, len);

fs.Close();
Stream respon = req.GetResponse().GetResponseStream();

StreamReader sr = new StreamReader(respon);

extractsendItLink(sr.ReadToEnd());

sr.Close();

respon.Close();
conn.Close();
Nov 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Du wrote:
I'm trying to automate the upload process to yousendit.com, but the
file size doesn't add up and yousendit.com keep rejecting my upload
(it accepts the upload until the very end)

I don't know what i'm missing, I use Fiddler and I got my header and
body to a very close match, but the content-length is slightly off
and I got no clue why

Sorry for the lengthy post, but i have no other way of showing my work

Can someone please explain the difference in content-length, I can't
seem to work it out. I been searching on google and the news group,
but i got nothing.


Check again with Fiddler. As far as I can see, you're at least missing
the final boundary:
---------------------------7d5158213e047e--

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 17 '05 #2

P: n/a
Du
I added the end boundary and still no go

any other though???

thanks

"Joerg Jooss" <ne********@joergjooss.de> wrote in message
news:xn****************@msnews.microsoft.com...
Du wrote:
I'm trying to automate the upload process to yousendit.com, but the
file size doesn't add up and yousendit.com keep rejecting my upload
(it accepts the upload until the very end)

I don't know what i'm missing, I use Fiddler and I got my header and
body to a very close match, but the content-length is slightly off
and I got no clue why

Sorry for the lengthy post, but i have no other way of showing my work

Can someone please explain the difference in content-length, I can't
seem to work it out. I been searching on google and the news group,
but i got nothing.


Check again with Fiddler. As far as I can see, you're at least missing
the final boundary:
---------------------------7d5158213e047e--

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de

Nov 17 '05 #3

P: n/a
Du wrote:
I added the end boundary and still no go

any other though???


Character encoding? You're using ASCII, but I bet IE isn't. This will
only be relevant if you post non-US-ASCII characters though. Is this
the case?

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 17 '05 #4

P: n/a
Du wrote:
I added the end boundary and still no go

any other though???


Character encoding? You're using ASCII, but I bet IE isn't. This will
only be relevant if you post non-US-ASCII characters though. Is this
the case?

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 17 '05 #5

P: n/a
Du
nope, it's all ascii, I also try utf-8 .. but still the same problem

Thanks Jooss, I really appreciate this
"Joerg Jooss" <ne********@joergjooss.de> wrote in message
news:xn****************@msnews.microsoft.com...
Du wrote:
I added the end boundary and still no go

any other though???


Character encoding? You're using ASCII, but I bet IE isn't. This will
only be relevant if you post non-US-ASCII characters though. Is this
the case?

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de

Nov 17 '05 #6

P: n/a
Du
nope, it's all ascii, I also try utf-8 .. but still the same problem

Thanks Jooss, I really appreciate this
"Joerg Jooss" <ne********@joergjooss.de> wrote in message
news:xn****************@msnews.microsoft.com...
Du wrote:
I added the end boundary and still no go

any other though???


Character encoding? You're using ASCII, but I bet IE isn't. This will
only be relevant if you post non-US-ASCII characters though. Is this
the case?

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de

Nov 17 '05 #7

P: n/a
Du wrote:
nope, it's all ascii, I also try utf-8 .. but still the same problem


Hm... did you post your entire code in your first post? We must be
missing something here.

Cheers,

--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 17 '05 #8

P: n/a
Du wrote:
nope, it's all ascii, I also try utf-8 .. but still the same problem


Hm... did you post your entire code in your first post? We must be
missing something here.

Cheers,

--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.