2

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

mans
  • 17,104
  • 45
  • 172
  • 321

2 Answers2

2

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:

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");
    }
}
Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486
  • Thanks. I like to know how can I create a form and post it (using the technique as you said in your 4th suggestion). Can you please elaborate or give some reference so I can do some study about it? – mans Mar 27 '14 at 13:16
  • @mans, I noticed a bunch of other recent `WebBrowser`-related questions from you. You've probably figured out how to do manipulate the form, have you? – noseratio Mar 29 '14 at 04:42
  • Not yet! I am doing some hacks for it to work, but it is an ugly way of doing this and I like to find a better way. In current implementation, I read web page, find form, fill fileds and then using javascript, click on all buttons and post it back. I lke to find a way to create the form on my code and post it without reading it from server as I know what is the format of form. Any suggestion on how to create a form and post it to server? – mans Mar 31 '14 at 08:16
  • @mans, check the update. This is as much as I could help with this question. – noseratio Mar 31 '14 at 08:32
  • 1
    Thanks. I think I can get it from here! – mans Mar 31 '14 at 08:36
0

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

Community
  • 1
  • 1
Eric Scherrer
  • 3,328
  • 1
  • 19
  • 34
  • Thanks, but then if I open the web Brower to point to the same url, is it still shows as a logged-in user or not? I think now, webRequest doesn't pas login cookie to webbrowser. I am wrong? – mans Mar 26 '14 at 18:16
  • Correct, different sandbox. Are you looking to open a browser, automatically fill it in and then post? – Eric Scherrer Mar 26 '14 at 18:20
  • Yes, I am looking for a way to open a webbrowser which is hosted inside my wpf application and use login information to login to the web site and show the output to user so he can work there. – mans Mar 27 '14 at 10:04
  • Check out the [WebBrowser Control](http://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser(v=vs.110).aspx). You may need to do some funky stuff to post though, like use the [UI Automation Library](http://msdn.microsoft.com/en-us/library/system.windows.automation.aspx) to fill out the form and post. – Eric Scherrer Mar 27 '14 at 13:29