如何使用 wpf webbrowser 将数据发布到 Web 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22669214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:21:35  来源:igfitidea点击:

How to post data to a web server using wpf webbrowser

c#phpwpfwebbrowser-control

提问by mans

I want to get data from a database and use it to login a user to a web site.

我想从数据库中获取数据并使用它来将用户登录到网站。

I have a wpf page that holds a web browser control. and I have this code that login user to web site which is written in php:

我有一个包含 Web 浏览器控件的 wpf 页面。我有这段代码可以让用户登录到用 php 编写的网站:

<form action='http://www.asite.net/index.php' method='post' name='frm'>
<?php
$user = $_GET['u'];
$pass = $_GET['p'];
echo "<input type='text' name='user' value='$user'>";
echo "<input type='text' name='pass' value='$pass'>";
?>
<input type='submit' name='submit' value='submit'>
</form>

How can I do this in wpf? As far as I can understand, I need to create an html and post it to site.

我怎样才能在 wpf 中做到这一点?据我所知,我需要创建一个 html 并将其发布到站点。

My questions:

我的问题:

1- How can I create such html in code?

1- 如何在代码中创建这样的 html?

2- How can I automatically submit it to the site (assuming I am doing this on constructor of a wpf user control).

2-如何自动将其提交到站点(假设我在 wpf 用户控件的构造函数上执行此操作)。

采纳答案by noseratio

As far as I understand, your goal is to log in and keep the session active inside the WebBrowser. If so, you have a few options:

据我了解,您的目标是登录并保持会话在WebBrowser. 如果是这样,您有几个选择:

Updated, here is an example of creating and submitting a :

更新,这里是一个创建和提交的例子:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += MainWindow_Loaded;
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        NavigatedEventHandler handler = null;
        handler = delegate
        {
            this.webBrowser.Navigated -= handler;

            dynamic document = this.webBrowser.Document;
            var form = document.createElement("form");
            form.action = "http://requestb.in/tox7drto";
            form.method = "post";

            var input = document.createElement("input");
            input.type = "text";
            input.name = "name_1";
            input.value = "value_1";
            form.appendChild(input);

            input = document.createElement("input");
            input.type = "submit";
            form.appendChild(input);

            document.body.appendChild(form);
            input.click();
        };

        this.webBrowser.Navigated += handler;
        this.webBrowser.Navigate("about:blank");
    }
}

回答by Eric Scherrer

Use the System.Netnamespace, particularly the WebRequestand WebResponseobjects.

使用System.Net命名空间,尤其是WebRequestWebResponse对象。

See this previous answer, it should get you started:

看到这个以前的答案,它应该让你开始:

How to programmatically fill a form and post a web page

如何以编程方式填写表单并发布网页