C# 使用Webclient发布/选择Multiselect的所有选项值
我正在尝试编写一个应用程序来自动配置路由器。不幸的是,对于我们正在使用的路由器,telnet不是一个选项 因此,我必须使用C WebClient类与Cisco web接口进行接口 到目前为止,我已经能够使用NameValueCollection和WebClient.UploadValues设置所需的一切 我将获取表单上的所有输入元素,然后上传表单上输入类型对应的名称值集合,将每个元素的值设置为所需的设置 但现在我遇到了一个问题 对于其中一个表单,它使用multiselect控件来处理输入数据数组,而不是输入类型 我完全不知道如何设置这个 multiselect的html如下所示C# 使用Webclient发布/选择Multiselect的所有选项值,c#,webclient,C#,Webclient,我正在尝试编写一个应用程序来自动配置路由器。不幸的是,对于我们正在使用的路由器,telnet不是一个选项 因此,我必须使用C WebClient类与Cisco web接口进行接口 到目前为止,我已经能够使用NameValueCollection和WebClient.UploadValues设置所需的一切 我将获取表单上的所有输入元素,然后上传表单上输入类型对应的名称值集合,将每个元素的值设置为所需的设置 但现在我遇到了一个问题 对于其中一个表单,它使用multiselect控件来处理输入数据数组
<select multiple class="MultiSelect" name="PortRangeList" size="12" onChange="showList(this.form.PortRangeList);" style="width: 100%">
<option value="All Traffic{[(*-*-*)]}1;0;1;65535;0}">All Traffic [TCP&UDP/1~65535]</option>
<option value="DNS{[(*-*-*)]}2;17;53;53;0}">DNS [UDP/53~53]</option>
<option value="FTP{[(*-*-*)]}3;6;21;21;0}">FTP [TCP/21~21]</option>
...
</select>
但是看看这个新表单的代码,它在提交之前就出现了,它选择了multiselect中的所有选项
我意识到我可能已经问过这个问题了,但如果能得到任何帮助,我将不胜感激
使用Chrome调试器PortRangeList完全如您所说 有5种输入类型 提交状态、upnpOpen等 对于这些,我的代码如下所示
NameValueCollection formData = new NameValueCollection();
formData["submitStatus"]="1";
formData["upnpOpen"]="0";
downloadedData = _routerWebClient.UploadValues(SERVICE0, formData);
但是为了提交PortRangeList数据,我不能使用NameValueCollection,因为它不允许名称具有多个值
你怎么能提交呢
WebClient.UploadData、WebClient.UploadFile或WebClient.UploadString可能?使用或比较正常浏览器和代码不工作时的连接方式。。。一旦知道了差异,您可以相应地更改代码…您必须通过多次传入PortRangeList参数来传入所选选项,每个选项一次:
PortRangeList=All Traffic{[(*-*-*)]}1;0;1;65535;0}&PortRangeList=DNS{[(*-*-*)]}2;17;53;53;0}&PortRangeList=FTP{[(*-*-*)]}3;6;21;21;0}
浏览器就是这样做的。由于您正在使用WebClient,请尝试以下操作:
PortRangeList=All Traffic{[(*-*-*)]}1;0;1;65535;0},DNS{[(*-*-*)]}2;17;53;53;0},FTP{[(*-*-*)]}3;6;21;21;0}
显然,所有内容都必须正确转义。我想我会发布最终答案 最后,我使用了这里所示的精确解。 这是他的代码,但我在没有使用全局变量的情况下做了完全相同的事情
StringBuilder _strBld = new StringBuilder();
int _intItemCount = 0;
protected void btnSubmit_Click(object sender, EventArgs e)
{
System.Net.WebClient myWebClient = new System.Net.WebClient();
myWebClient.Headers.Add("Charset", "text/html; charset=UTF-8");
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); // ◄ This line is essential
// Perform server-side validations (same as before)
if (this.F_Name.Text.Length == 0 || this.L_Name.Text.Length == 0)
{ AppendError("First and Last name must be provided"); }
…
// Add the user-provided name values
AppendUploadString("last_name", this.L_Name.Text);
AppendUploadString ("first_name", this.F_Name.Text);
AppendUploadString ("address", this.Address.Text);
// Add the Toppings
foreach (ListItem item in this.ToppingsChkBoxList.Items)
{
if (item.Selected)
{
AppendUploadString("Toppings", item.Value.ToString());
}
}
myWebClient.UploadString("https http://www.Destination.com/...?encoding=UTF-8", "POST", _strBld.ToString());
}
private void AppendUploadString(string strName, string strValue)
{
_intItemCount++;
_strBld.Append((intItemCount == 1 ? "" : "&") + strName + "=" + System.Web.HttpUtility.UrlEncode(strValue));
// Update: Use UrlEncode to ensure that the special characters are included in the submission
}
谢谢你的提示。最后,我发现Chrome内置了查看正在发布的表单数据的功能。使用Chrome调试器PortRangeList正是您所说的。有5种输入类型submitStatus、upnpOpen等。。。对于那些我的代码看起来像这样的NameValueCollection formData=new NameValueCollection;formData[submitStatus]=1;formData[upnpOpen]=0;downloadedData=_routerWebClient.UploadValuesSERVICE0,formData;但是为了提交PortRangeList数据,我不能使用NameValueCollection,因为它不允许名称具有多个值。你怎么能提交呢?WebClient.UploadData、WebClient.UploadFile或WebClient.UploadString?请再次阅读我的答案,尤其是第二个代码块-只需将值与、逗号和作为单个值传入即可。
StringBuilder _strBld = new StringBuilder();
int _intItemCount = 0;
protected void btnSubmit_Click(object sender, EventArgs e)
{
System.Net.WebClient myWebClient = new System.Net.WebClient();
myWebClient.Headers.Add("Charset", "text/html; charset=UTF-8");
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); // ◄ This line is essential
// Perform server-side validations (same as before)
if (this.F_Name.Text.Length == 0 || this.L_Name.Text.Length == 0)
{ AppendError("First and Last name must be provided"); }
…
// Add the user-provided name values
AppendUploadString("last_name", this.L_Name.Text);
AppendUploadString ("first_name", this.F_Name.Text);
AppendUploadString ("address", this.Address.Text);
// Add the Toppings
foreach (ListItem item in this.ToppingsChkBoxList.Items)
{
if (item.Selected)
{
AppendUploadString("Toppings", item.Value.ToString());
}
}
myWebClient.UploadString("https http://www.Destination.com/...?encoding=UTF-8", "POST", _strBld.ToString());
}
private void AppendUploadString(string strName, string strValue)
{
_intItemCount++;
_strBld.Append((intItemCount == 1 ? "" : "&") + strName + "=" + System.Web.HttpUtility.UrlEncode(strValue));
// Update: Use UrlEncode to ensure that the special characters are included in the submission
}