[英]How can we post same parameter name multiple times using HttpWebRequest in multipart/form-data in C#?
[here is exmple] [这里举例]
I want to post same parameter multiple times but value is different using HttpWebRequest in multipart/form-data. 我想多次发布相同的参数,但是在multipart / form-data中使用HttpWebRequest的值不同。
If i am post same parameter multiple times then its overwrite with last value but it post single times. 如果我多次发布相同的参数,那么它会覆盖上一次的值,但是会发布一次。 can any one help me with that. 谁能帮助我。 below is my HttpWebRequest code : 下面是我的HttpWebRequest代码:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
var boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.Method = "POST";
request.KeepAlive = true;
Stream memStream = new System.IO.MemoryStream();
var endBoundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
boundary + "--");
string formdataTemplate = "\r\n--" + boundary +
"\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
if (data != null)
{
foreach (string key in data.Keys)
{
string formitem = string.Empty;
string[] values = data.GetValues(key);
foreach (string value in values)
{
formitem = string.Format(formdataTemplate, key, value);
}
formitem = string.Format(formdataTemplate, key, key);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
memStream.Write(formitembytes, 0, formitembytes.Length);
}
}
memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
request.ContentLength = memStream.Length;
using (Stream requestStream = request.GetRequestStream())
{
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
}
Try this code, that uses MultipartFormDataContent . 尝试使用MultipartFormDataContent的此代码。
I created a simple helper function that adds parameters to the form. 我创建了一个简单的辅助函数,该函数将参数添加到表单中。 In this example, it adds 2 parameters "name"
with 2 different value. 在此示例中,它添加了两个带有两个不同值的参数"name"
。
public static void PostSomething()
{
var url = "http://localhost:5591/api/values";
using (var client = new HttpClient())
{
using (var formData = new MultipartFormDataContent())
{
formData.Add(AddContentDispositionParam("name", "val1"));
formData.Add(AddContentDispositionParam("name", "val2"));
try
{
var response = client.PostAsync(url, formData).Result;
}
catch (Exception e)
{
}
}
}
}
public static StringContent AddContentDispositionParam(string name, string value)
{
var stringContent = new StringContent(value, Encoding.UTF8);
stringContent.Headers.Add("Content-Disposition", $"form-data; name=\"{name}\"");
return stringContent;
}
In the server code, I have ASP.NET Core 2.2 Post handler. 在服务器代码中,我具有ASP.NET Core 2.2 Post处理程序。
[HttpPost]
[AllowAnonymous]
public void Post()
{
foreach (var formField in Request.Form)
{
// Form data
var values = formField.Value;
foreach (var v in values)
{
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.