如何使用 C# 检索 Web 窗体上已有的数据

本文关键字:数据 窗体 何使用 检索 Web | 更新日期: 2023-09-27 18:36:08

我正在开发一个访问路由器页面并检索数据(特别是wifi安全代码)的程序 它不是黑客工具,因为它只有在您的计算机已经成功连接到它时才有效。 问题是,当我想读取页面上已有的数据(在类似文本框中)时,我必须使用正则表达式,因为路由器总是返回 html 源代码而不是 xml 数据表(即使我将 ContentType 设置为 text/xml)。

有没有办法类似于我的代码,将数据写入从网络表单读取数据的网络表单。 这是我的代码示例,它写入"密码"字段并将其提交给路由器。

string formParams = string.Format("password={0}", loginInput);
WebRequest loginPage = WebRequest.Create(url);
loginPage.ContentType = "application/x-www-form-urlencoded";
loginPage.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
loginPage.ContentLength = bytes.Length;
using (Stream os = loginPage.GetRequestStream())
{
   os.Write(bytes, 0, bytes.Length);
}

下面是网页中 html 代码的摘录,该代码在其中写入 loginInput 中的数据。

   <td class="form_label">Password&nbsp;:</td>
<td class="form_data">
<input type="password" id="password" name="password" value="" tabindex="100" />
</td>

如何使用 C# 检索 Web 窗体上已有的数据

使用 HTML 敏捷包

例如,以下是修复 HTML 文件中的所有 href 的方法:

HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm");
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
{
    HtmlAttribute att = link["href"];
    att.Value = FixLink(att);
}
doc.Save("file.htm"); 

它使用 XPATH 来选择 html 部件,但也可以使用 LINQ。有很多示例如何使用它