C#登录网页而不打开它

时间:2017-10-09 09:19:30

标签: c# wpf web

每个人

我正在寻找一个可以帮助我解决问题的想法。我正在尝试创建一个wpf程序,我可以在WPF中使用给定的用户名和密码登录网页,之后我也可以获得成功的消息。

我尝试使用以下代码:

Xaml文件:

<StackPanel Orientation="Horizontal" Background="Black">
        <TextBox x:Name="username" Width="200" Margin="2,2,10,2" Text="username"/>
        <TextBox x:Name="password" Width="200" Margin="2,2,10,2" Text="password"/>
        <Button Width="200" Content="Go!" Margin="2" Click="Button_Click" />
    </StackPanel>
    <StackPanel Orientation="Vertical" Grid.Row="1" >
        <WebBrowser x:Name="wb1" Height="500" />
        <TextBlock x:Name="state" Text="Connection" ></TextBlock>
    </StackPanel>

Code Behinde:

...

    public MainWindow()
    {
        InitializeComponent();

        wb1.LoadCompleted += new LoadCompletedEventHandler(bws_LoadCompleted);
        wb1.Navigated += new NavigatedEventHandler(bws_Navigated);

    }

           void bws_Navigated(object sender, NavigationEventArgs e)
    {
        HideScriptErrors(wb1, true);
    }

    void bws_LoadCompleted(object sender, NavigationEventArgs e)
    {
        switch (e.Uri.AbsolutePath)
        {
            case "/":
                DoExample1();
                break;
            case "/login":
                DoExample1();
                break;
        }
    }

    private void DoExample1()
    {
        TryFillInBing(username.Text, password.Text);

    }

    void TryFillInBing(string name, string passwd)
    {
        Thread.Sleep(500);
        Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
        {
            if (!LoginInput(0, "username", "password", name, passwd) && Tries < 6)
            {
                Tries++;
                TryFillInBing(name, passwd);
            }
            else
            {
                IHTMLFormElement form = GetForm(0);
                form.submit();
            }
        }));
    }

    private bool UpdateTextInput(int formId, string name, string text)
    {
        bool successful = false;
        IHTMLFormElement form = GetForm(formId);
        if (form != null)
        {
            var element = form.item(name: name);
            if (element != null)
            {
                var textinput = element as HTMLInputElement;
                textinput.value = text;
                successful = true;
            }
        }

        return successful;
    }

    private bool LoginInput(int formId, string name, string passwd, string text, string pw)
    {
        bool successful = false;
        IHTMLFormElement form = GetForm(formId);
        if (form != null)
        {
            var element = form.item(name);
            var pass = form.item(passwd);

            if (element != null && pass != null)
            {
                var textinput = element as HTMLInputElement;
                textinput.value = text;
                var passinput = pass as HTMLInputElement;
                passinput.value = pw;

                successful = true;
            }
        }
        return successful;
    }

... ... ...

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        string conquerclubLogin = "https://www.conquerclub.com";
        string googleWeb = "http://www.google.de";

        wb1.Navigate(conquerclubLogin);
    }

但它向我显示了相同的登录页面,我没有收到任何成功登录的消息......有人对我有任何想法吗?

干杯,卡文

0 个答案:

没有答案