Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

c# - Send multipart/form-data content type request

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

-----------------------------27311326571405
Content-Disposition: form-data; name="list"

8274184
-----------------------------27311326571405
Content-Disposition: form-data; name="list"

8274174
-----------------------------27311326571405
Content-Disposition: form-data; name="list"

8274178
-----------------------------27311326571405
Content-Disposition: form-data; name="antirobot"

2341234
-----------------------------27311326571405
Content-Disposition: form-data; name="votehidden"

1
-----------------------------27311326571405--

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

---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 = "----MyAppBoundary" + 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());
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...