Issue with HttpWebRequest & multipart/form-data

D

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"

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

(e-mail address removed)
---------------------------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"

dDwtMTgyNzA1MTg5OztsPFVwbG9hZEZpbGU7Pj6IqrAZhsvVRc9Qe1y6cNu6G3V3nQ==
-----------------------------7d533333730406
Content-Disposition: form-data; name="ToEMail"

(e-mail address removed)
-----------------------------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\ndDwtMTgyNzA1MTg5OztsPFVwbG9hZEZpbGU7Pj6IqrAZhsvVRc9Qe1y6cNu6G3V3nQ==\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();
 
J

Joerg Jooss

Du said:
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,
 
J

Joerg Jooss

Du said:
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,
 
D

Du

nope, it's all ascii, I also try utf-8 .. but still the same problem

Thanks Jooss, I really appreciate this
 
J

Joerg Jooss

Du said:
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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top