I've written the following code in order to construct a POST, using the PostData class I found on stackoverflow.
PostData pd = new PostData();
pd.Params.Add(new PostDataParam("sessionId", "0", PostDataParamType.Field));
pd.Params.Add(new PostDataParam("guestId", "1", PostDataParamType.Field));
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(new Uri("http://oe1235/test/uploadTest.php").AbsoluteUri);
webrequest.ContentType = "multipart/form-data; boundary=" + pd.Boundary;
webrequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
webrequest.Headers.Add("Accept-Language: en-gb,en;q=0.5");
webrequest.Headers.Add("Accept-Encoding: gzip,deflate");
webrequest.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
webrequest.Headers.Add("Keep-Alive: 115");
webrequest.Referer = "http://localhost/test/test.php";
webrequest.Headers.Add("Cache-Control: max-age=0");
webrequest.Method = "POST";
byte[] content = Encoding.ASCII.GetBytes(pd.GetPostData());
webrequest.ContentLength = content.Length;
Stream request = webrequest.GetRequestStream();
request.Write(content, 0, content.Length);
try {
Console.Write(webrequest.GetResponse());
} catch (Exception e) {
Console.Write("Error: " + e.ToString());
}
Console.ReadLine();
I'm monitoring the request / response with Charles, and I get the following:
POST /test/uploadTest.php HTTP/1.1
Content-Type: multipart/form-data; boundary=----------8cd4899a18b409a
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Referer: http://localhost/test/test.php
Cache-Control: max-age=0
Host: oe1235
Content-Length: 275
Expect: 100-continue
----------8cd4899a18b409a
Content-Disposition: form-data; name="email"
MyEmail
----------8cd4899a18b409a
Content-Disposition: form-data; name="sessionId"
0
----------8cd4899a18b409a
Content-Disposition: form-data; name="guestId"
1
----------8cd4899a18b409a--
I've tried everything I can think of, but it still doesn't appear to be valid (no response from my POST-receiver, and Charles reports "Failed to decode Multipart body", so I've done something wrong. My boundary looks ok, I've got line breaks in all the correct places, I've compared it to a POST that Firefox constructs and they look (pretty much - apart from one or two headers) identical.