Building POST in C #

I wrote the following code to build a POST using the PostData class that 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 am tracking 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 tried everything I could think of, but it still seems invalid (there is no response from my POST receiver, and Charles reports β€œFailed to decode the Multipart body”, so I did something wrong. My border looks fine, I have line breaks in all the right places, I compared it to the POST that Firefox built, and they look (to a large extent - except for one or two headers) the same.

+3
source share
3 answers

- , , , . , , :

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

:

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

, :

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

:

--myboundary
+3

Check this one . This is a response from a community that didn't let me down. You can also check the MSDN library .

0
source

Source: https://habr.com/ru/post/1772636/


All Articles