How to post data to a web server using wpf webbrowser

你说的曾经没有我的故事 提交于 2019-12-08 02:49:20

问题


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:

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

My questions:

1- How can I create such html in code?

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


回答1:


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:

  • First, navigate the WebBrowser to www.asite.net, to establish the session.

  • Then obtain the underlying WebBrowser ActiveX control and use IWebBrowser2::Navigate2 method, it has PostData parameter which allows to do an HTTP POST request.

  • Or, inject and execute some JavaScript which would use XHR to post the form the AJAX way.

  • Or, use WebBrowser.Document as dynamic to create a hidden form element, populate it and submit it, in the same way you'd do with JavaScript.

  • Or, use COM XMLHTTPobject to send a POST request, it shares the session with the WebBrowser.

  • You could also use some low level UrlMon API to send a POST request.

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");
    }
}



回答2:


Use the System.Net namespace, particularly the WebRequest and WebResponse objects.

See this previous answer, it should get you started:

How to programmatically fill a form and post a web page



来源:https://stackoverflow.com/questions/22669214/how-to-post-data-to-a-web-server-using-wpf-webbrowser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!