以 MVVM 方式从 C# 提交 POST 表单

发布于 2024-11-15 17:23:10 字数 885 浏览 6 评论 0原文

我正在使用 WebBrowser 控件来自动管理网页。当我启动 WPF 应用程序时,它显示网页 ant 让我登录。然后我的应用程序开始通过转到页面、填写表单并提交(这是一个 POST 表单)来执行一些自动化任务。它使用不同的值提交约 100 次相同的表单。

现在我的代码如下:

void webBrowser1_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
    var doc = (HTMLDocument) webBrowser1.Document;
    var form = doc.getElementById("theForm");

    SetValueOfFormInput(doc, "id", Foo.ID);
    SetValueOfFormInput(doc, "item1", Foo.Item1);
    SetValueOfFormInput(doc, "item2", Foo.Item2);
    form.all.item("submit").click();
}

private static void SetValueOfFormInput(HTMLDocument doc, string name, string value)
{
    var item = doc.all.item(name);
    item.setAttribute("value", value);
}

我可以用更好的方式做到这一点吗?我可以用MVVM方式做到这一点吗?

不,我无法修改网页来使管理更容易:-(

编辑: 理想情况下,我无需使用 WebBrowser 控件即可完成此操作。程序登录网站并执行所有任务,无需修改 html 页面中的表单

I'm using a WebBrowser control to automate management of a web page. When I start my WPF application, it shows the webpage ant lets me login. Then my application starts to do some automated tasks by going to a page, filling a form and submitting (it’s a POST-form). It submits the same form ~100 times with different values.

Right now my code is as follows:

void webBrowser1_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
    var doc = (HTMLDocument) webBrowser1.Document;
    var form = doc.getElementById("theForm");

    SetValueOfFormInput(doc, "id", Foo.ID);
    SetValueOfFormInput(doc, "item1", Foo.Item1);
    SetValueOfFormInput(doc, "item2", Foo.Item2);
    form.all.item("submit").click();
}

private static void SetValueOfFormInput(HTMLDocument doc, string name, string value)
{
    var item = doc.all.item(name);
    item.setAttribute("value", value);
}

Can I do this on a better way and can I do it in a MVVM way?

And no, I can't modify the the webpage to do the management easier :-(

Edit:
Ideally, I would be able to do this without having to use the WebBrowser control. The program logon to the website and performs all tasks without having to modify the forms in the html pages

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有深☉意 2024-11-22 17:23:10

为什么不使用 WebClient 或 WebRequest 类?
对于网络客户端,您可以使用 UploadValues 方法,该方法将完全执行您想要的操作 (http://msdn.microsoft.com/en-us/library/9w7b4fz7.aspx),您也可以简单地添加类以使用 cookie,以便您的登录将是“永久”的(http://stackoverflow.com/questions/1777221/c-using-cookiecontainer-with-webclient-class)

如果您喜欢更模型在驱动下,我将使用 WebRequest(已经准备好一个 cookie 容器)并使用一些包含所需数据的类。这个派生自一个类,它可以将所有需要的属性序列化为一个简单的字符串,您可以将其发布到服务器 - AFAIK 它与 getter 参数相同(param1=val1¶m2=val2&...)
所以基本上:

class Data : Postable { public string Param1{get;set;} public string Param2{get;set;} ...}
class Postable 
{ 
    public override string ToString() 
    { 
        StringBuilder ret = new StringBuilder();
        foreach(Property p in GetType().GetProperties())
        {
            ret.Append("{0}={1}&", p.Name, p.<GetValue>);
        }
        return ret.ToString();
    } 
}

Why not using the WebClient or WebRequest classes?
For the webclient, you can use the UploadValues method which will do exactly what you want (http://msdn.microsoft.com/en-us/library/9w7b4fz7.aspx) and you can also simply addapt the class to use cookies so your login will be "permanent" (http://stackoverflow.com/questions/1777221/c-using-cookiecontainer-with-webclient-class)

If you like to do it even more model driven, i would use the WebRequest (has allready a cookiecontainer) and have some class with the needed data. This one would derive from a class, which can serialize all needed properties into a simple string you would post to the server - AFAIK it's same like the getter-parameters (param1=val1¶m2=val2&...)
so basically:

class Data : Postable { public string Param1{get;set;} public string Param2{get;set;} ...}
class Postable 
{ 
    public override string ToString() 
    { 
        StringBuilder ret = new StringBuilder();
        foreach(Property p in GetType().GetProperties())
        {
            ret.Append("{0}={1}&", p.Name, p.<GetValue>);
        }
        return ret.ToString();
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文