views:

67

answers:

2

Hi

The follow http post request send data using multipart/form-data content type.

-----------------------------27311326571405\r\nContent-Disposition: form-data; name="list"\r\n\r\n8274184\r\n-----------------------------27311326571405\r\nContent-Disposition: form-data; name="list"\r\n\r\n8274174\r\n-----------------------------27311326571405\r\nContent-Disposition: form-data; name="list"\r\n\r\n8274178\r\n-----------------------------27311326571405\r\nContent-Disposition: form-data; name="antirobot"\r\n\r\n2341234\r\n-----------------------------27311326571405\r\nContent-Disposition: form-data; name="votehidden"\r\n\r\n1\r\n-----------------------------27311326571405--\r\n

List is an input name. 8274184, 8274174, 8274178 etc are input value. But what is 27311326571405, 27311326571405...etc? I want to send same request using c# but i really donnt know where i can to get this numbers.

+1  A: 

---27311326571405 is called boundary and it is a random string that should never appear in the data you are sending and is used as separator between the values.

Here's an example of sending such a request to a given address:

class Program
{
    static void Main()
    {
        var data = new List<KeyValuePair<string, string>>(new[]
        {
            new KeyValuePair<string, string>("list", "8274184"),
            new KeyValuePair<string, string>("list", "8274174"),
            new KeyValuePair<string, string>("list", "8274178"),
            new KeyValuePair<string, string>("antirobot", "2341234"),
            new KeyValuePair<string, string>("votehidden", "1"),
        });

        string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");

        var request = (HttpWebRequest)WebRequest.Create("http://example.com");
        request.ContentType = "multipart/form-data; boundary=" + boundary;
        request.Method = "POST";

        using (var requestStream = request.GetRequestStream())
        using (var writer = new StreamWriter(requestStream))
        {
            foreach (var item in data)
            {
                writer.WriteLine(boundary);
                writer.WriteLine(string.Format("Content-Disposition: form-data; name=\"{0}\"", item.Key));
                writer.WriteLine();
                writer.WriteLine(item.Value);
            }
            writer.WriteLine(boundary + "--");
        }

        using (var response = request.GetResponse())
        using (var responseStream = response.GetResponseStream())
        using (var reader = new StreamReader(responseStream))
        {
            Console.WriteLine(reader.ReadToEnd());
        }
    }
}
Darin Dimitrov
A: 

It is a semi-random string, used to distinguish the different fields. In the content-type header this is given as the boundary.

see what-are-valid-characters-for-creating-a-multipart-form-boundary

mvds