使用Webclient POST /选择Multiselect的所有选项值

时间:2012-06-11 20:57:49

标签: c# webclient

我正在尝试编写一个应用程序来自动化路由器配置。不幸的是,对于我们使用的路由器,telnet不是一个选项。

所以我不得不使用C#WebClient类与思科Web界面连接。

到目前为止,我已经能够使用NameValueCollection和WebClient.UploadValues设置我需要的所有内容。

我将获取表单上的所有输入元素,然后只需上传与表单上的输入类型相对应的名称值Collection,将每个值设置为所需的设置。

但现在我遇到了一个问题。

使用其中一个表单,它使用多选控件来处理输入数据数组,而不是输入类型。

我完全不知道如何设置它。

多选的html如下

<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> 

当我使用输入类型时,我会简单地执行以下操作

NameValueCollection formNetworkData = new NameValueCollection();
formNetworkData["ipAddr"] = "192.168.1.2";
formNetworkData["lanMask"] = "255.255.255.0";
downloadedData = _routerWebClient.UploadValues(_routerIP + NETWORK, formNetworkData);

但是看一下这个新表单的代码,它在提交之前就会出现,它会选择多选中的所有选项。

我意识到我可能已经非常地问过这个问题了,但是非常感谢任何帮助。


使用Chrome调试器PortRangeList就像你说的那样。

有5种输入类型

submitStatus,upnpOpen(etc ...)

对于那些我的代码看起来像这样

NameValueCollection formData = new NameValueCollection();
formData["submitStatus"]="1";
formData["upnpOpen"]="0";
downloadedData = _routerWebClient.UploadValues(SERVICE0, formData);

但是为了提交PortRangeList数据,我不能使用NameValueCollection,因为它不允许名称具有多个值。

怎么可以提交?

WebClient.UploadData,WebClient.UploadFile或WebClient.UploadString可能吗?

3 个答案:

答案 0 :(得分:0)

使用FiddlerWireshark来比较线路工作时的线路(“正常”浏览器)和不工作时(您的代码)......一旦您知道差异你可以相应地改变你的代码......

答案 1 :(得分:0)

您必须通过多次传递“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}

显然,所有内容都必须正确进行网址转义。

答案 2 :(得分:0)

以为我会发布最终答案。

最后我使用了这里显示的确切解决方案。 http://anhonga.wordpress.com/2010/05/06/using-webclient-with-uploadvalues-and-uploadstring-to-simulate-post/

这是他的代码,但我基本上做了完全相同的事情(不使用全局变量)

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
}