natron3k
asked on
HTTPWebRequest with multipart/form-data post to php leaves form values blank
So I've got a HTTPWebRequest based app written in C# that posts to php page. The file makes it to the web site no problem, but the form variables are blank. The names of the form variables are being passed, but not the values. It's the craziest thing. Below is a sample of one of the form variables being attached. Can anyone spot something obvious in the way it's being formatted?
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(_posturl);
webRequest.UserAgent = "Mozilla/5.0";
webRequest.ContentType = "multipart/form-data; boundary=" + dataBoundary;
webRequest.Method = "POST";
webRequest.KeepAlive = true;
// webRequest.Credentials = credscache;
// webRequest.PreAuthenticate = true;
//webRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("becu_photo:album")));
// Let's try and trick the stupid thing by delcaring the referer to be on the same server
//webRequest.Referer = "http://192.168.81.108/BECUupload.php";
// Declare the form data for the file coming through the request
// The file is expected to be called userphoto
// Every piece of a multipart/form-data has to be seperated by a dataBoundary which is a GUID in this case
string headerStr = String.Format(
"--{0}\r\n"
+ "Content-Disposition: form-data; name=\"userpicture\"; filename=\"{1}\"\r\n"
+ "Content-Type: application/octet-stream\r\n"
+ "\r\n",
dataBoundary, fileInfo.Name);
byte[] headerBytes = Encoding.Default.GetBytes(headerStr);
string trailerStr = String.Format("\r\n--{0}--\r\n", dataBoundary);
byte[] trailerBytes = Encoding.Default.GetBytes(trailerStr);
//Add the file header and trailer byte length to the total form post size
streamsize += headerBytes.Length;
streamsize += trailerBytes.Length;
streamsize += fileInfo.Length;
_formvars = new List<byte[]>();
string oheaderStr = "";
// Add the Max_File_Size var which is hidden in the form
oheaderStr = String.Format("--{0}\r\n"
+ "Content-Disposition: form-data; name=\"MAX_FILE_SIZE\""
+ "\r\n\r\n"
+ "102424576"
+ "\r\n",
dataBoundary);
_formvars.Add(Encoding.Default.GetBytes(oheaderStr));
streamsize += Encoding.Default.GetBytes(oheaderStr).Length;
streamsize += trailerBytes.Length;
// Add the album variable which is 27 for the Queue
oheaderStr = String.Format("--{0}\r\n"
+ "Content-Disposition: form-data; name=\"album\""
+ "\r\n\r\n"
+ "27"
+ "\r\n",
dataBoundary);
_formvars.Add(Encoding.Default.GetBytes(oheaderStr));
streamsize += Encoding.Default.GetBytes(oheaderStr).Length;
streamsize += trailerBytes.Length;
// Add the control variable which is phase_1
// Not sure wtf that means
oheaderStr = String.Format("--{0}\r\n"
+ "Content-Disposition: form-data; name=\"control\""
+ "\r\n\r\n"
+ "phase_1"
+ "\r\n",
dataBoundary);
_formvars.Add(Encoding.Default.GetBytes(oheaderStr));
streamsize += Encoding.Default.GetBytes(oheaderStr).Length;
streamsize += trailerBytes.Length;
// Add user1 variable which takes all the user data and concats with a ,
string user1 = "";
user1 += _formdata["firstname"] + ",";
user1 += _formdata["lastname"] + ",";
user1 += _formdata["city"] + ",";
user1 += _formdata["postalcode"] + ",";
user1 += _formdata["phonenumber"] + ",";
user1 += _formdata["emailaddress"];
oheaderStr = String.Format("--{0}\r\n"
+ "Content-Disposition: form-data; name=\"user1\""
+ "\r\n\r\n"
+ user1
+ "\r\n",
dataBoundary);
_formvars.Add(Encoding.Default.GetBytes(oheaderStr));
streamsize += Encoding.Default.GetBytes(oheaderStr).Length;
streamsize += trailerBytes.Length;
// Add Description
oheaderStr = String.Format("--{0}\r\n"
+ "Content-Disposition: form-data; name=\"caption\""
+ "\r\n\r\n"
+ _formdata["caption"]
+ "\r\n",
dataBoundary);
_formvars.Add(Encoding.Default.GetBytes(oheaderStr));
streamsize += Encoding.Default.GetBytes(oheaderStr).Length;
streamsize += trailerBytes.Length;
// Add hidden value called event
oheaderStr = String.Format("--{0}\r\n"
+ "Content-Disposition: form-data; name=\"event\""
+ "\r\n\r\n"
+ "picture"
+ "\r\n",
dataBoundary);
_formvars.Add(Encoding.Default.GetBytes(oheaderStr));
streamsize += Encoding.Default.GetBytes(oheaderStr).Length;
streamsize += trailerBytes.Length;
webRequest.ContentLength = streamsize;
Stream webStream = webRequest.GetRequestStream();
// write the header to the web stream
WriteStream(headerBytes, trailerBytes, webStream, fs);
WriteResponse((HttpWebResponse)webRequest.GetResponse());
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER