以下http post请求使用multipart / form-data内容类型发送数据.
-----------------------------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
列表是输入名称. 8274184,8274174,8274178等是输入值.但是什么是27311326571405,27311326571405 ……等等?我想用c#发送相同的请求,但我真的不知道在哪里可以得到这个数字.
解决方法
— 27311326571405被称为边界,它是一个随机字符串,不应出现在您要发送的数据中,并用作值之间的分隔符.
以下是将此类请求发送到给定地址的示例:
class Program { static void Main() { var data = new List<KeyValuePair<string,string>>(new[] { new KeyValuePair<string,string>("list","8274184"),new KeyValuePair<string,"8274174"),"8274178"),string>("antirobot","2341234"),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()); } } }