Asp.net C# post multipart/form-data 如何提交同名参数?

XiGua_DaShu 2018-07-09 08:17:49
如下三个参数的name="ABC"是一样的:
-----------------------------7e21e335d0b4e
Content-Disposition: form-data; name="ABC"

01
-----------------------------7e21e335d0b4e
Content-Disposition: form-data; name="ABC"

02
-----------------------------7e21e335d0b4e
Content-Disposition: form-data; name="ABC"

03
...全文
537 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
XiGua_DaShu 2018-07-11
  • 打赏
  • 举报
回复
我做成页面提交了,可以解决同名参数的问题。
但是Cookie带不过去,域名完全是两个域名,不是子域名

<form enctype="multipart/form-data" method="post" name="epolicyForm" action="http://e.abcde.com/d.aspx">
XiGua_DaShu 2018-07-09
  • 打赏
  • 举报
回复
引用 5 楼 sp1234 的回复:
你“提交”?

不用编程。后台接收数据时本来就应该自动区分收到的对象是字符串还是字符串数组。你的后台代码贴出来看看。


贴出来了
XiGua_DaShu 2018-07-09
  • 打赏
  • 举报
回复
我吧代码贴出来,方便分析
调用:

public void Sub()
{
NameValueCollection myCol = new NameValueCollection();
myCol.Add("ABC", "01");
myCol.Add("ABC", "02");
myCol.Add("ABC", "03");
string toubaourl = "http://e.abcde.com/d.aspx";
string toubaoresult = PostResponse(toubaourl, myCol,Encoding.GetEncoding("GB2312"));
Response.Write(toubaoresult);
}

执行:

public static string PostResponse(string url, NameValueCollection input, Encoding endoding)
{

string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.Method = "POST";
request.KeepAlive = true;
//request.Credentials = CredentialCache.DefaultCredentials;
request.Expect = "";
CookieContainer cookieCon = new CookieContainer();
request.CookieContainer = cookieCon;
request.CookieContainer.SetCookies(new Uri(url), cookieheader);
MemoryStream stream = new MemoryStream();


byte[] line = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
byte[] enterER = Encoding.ASCII.GetBytes("\r\n");

//提交文本字段
if (input != null)
{
string format = "--" + boundary + "\r\nContent-Disposition:form-data;name=\"{0}\"\r\n\r\n{1}\r\n";
foreach (string key in input.Keys)
{
string s = string.Format(format, key, input[key]);
byte[] data = Encoding.UTF8.GetBytes(s);
stream.Write(data, 0, data.Length);
}
}

byte[] foot_data = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n"); //项目最后的分隔符字符串需要带上--
stream.Write(foot_data, 0, foot_data.Length);
request.ContentLength = stream.Length;
Stream requestStream = request.GetRequestStream(); //写入请求数据
stream.Position = 0L;
stream.CopyTo(requestStream);
stream.Close();
requestStream.Close();
try
{
HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse();

try
{
using (var responseStream = response.GetResponseStream())
using (var mstream = new MemoryStream())
{
responseStream.CopyTo(mstream);
string message = endoding.GetString(mstream.ToArray());
return message;
}
}
catch (Exception ex)
{
throw ex;
}
}
catch (WebException ex)
{

throw ex;
}


}
catch (Exception ex)
{
throw ex;
}
}
  • 打赏
  • 举报
回复
你“提交”?

不用编程。后台接收数据时本来就应该自动区分收到的对象是字符串还是字符串数组。你的后台代码贴出来看看。
XiGua_DaShu 2018-07-09
  • 打赏
  • 举报
回复
01,02,03
这样,刚才那个多了个逗号
XiGua_DaShu 2018-07-09
  • 打赏
  • 举报
回复
引用 1 楼 sp1234 的回复:
name 重名本来就是支持的,多个 html element 的 name 可以重复,在接收端会作为数组(或者集合,根据你使用的处理框架类库而定)处理。


抓取原网站数据包是这样的:
-----------------------------7e21e335d0b4e
Content-Disposition: form-data; name="ABC"

01
-----------------------------7e21e335d0b4e
Content-Disposition: form-data; name="ABC"

02
-----------------------------7e21e335d0b4e
Content-Disposition: form-data; name="ABC"

03

我按照㕒格式提交是这样的:
-----------------------------7e21e335d0b4e
Content-Disposition: form-data; name="ABC"

01,,02,03
Logerlink 2018-07-09
  • 打赏
  • 举报
回复
不清楚你给的例子是什么意思
不过在一次post请求中,只要有相同的参数名称,后台接收数据都会使用逗号拼接起来
  • 打赏
  • 举报
回复
name 重名本来就是支持的,多个 html element 的 name 可以重复,在接收端会作为数组(或者集合,根据你使用的处理框架类库而定)处理。

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧