3

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.

jaucourt
  • 566
  • 4
  • 19

3 Answers3

3

Worked it out - I assumed that when the boundary was specified, it should include the two leading -- characters, when it shouldn't. So my boundary specified on the content header should be:

----------8cd4899a18b409a

and then between each item:

------------8cd4899a18b409a 

More simply, if your boundary was:

Content-Type: multipart/form-data; boundary=myboundary

Then between each item it should be:

--myboundary
jaucourt
  • 566
  • 4
  • 19
1

Maybe the problem is that there are two dashes missing at the start of the boundaries.

See http://en.wikipedia.org/wiki/MIME#Multipart_messages for more information on how the multipart body should look.

Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111
  • Yeah - I didn't realise that the two leading dashes *shouldn't* be included in the boundary specification within the Content-Type header. – jaucourt Nov 02 '10 at 19:05
0

Check this out. This is an answer from a community that never let me down. You may also check MSDN library.

radu florescu
  • 4,315
  • 10
  • 60
  • 92