1

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

所以我不得不使用 C# WebClient 类与 Cisco 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 (等等...)

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

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

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

怎么能提交呢?

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

4

3 回答 3

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}

显然,一切都必须正确地进行 URL 转义。

于 2012-06-11T21:06:31.897 回答
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
}
于 2012-06-13T12:47:48.283 回答
0

使用FiddlerWireshark来比较网络上的内容(“正常”浏览器)和不工作时(您的代码)...一旦您知道差异,您就可以相应地更改您的代码...

于 2012-06-11T21:06:14.953 回答