如何使用WPF web浏览器发布数据到Web服务器 [英] How to post data to a web server using wpf webbrowser
问题描述
我想从数据库中获取数据,并用它来登录用户到网站。
I want to get data from a database and use it to login a user to a web site.
我有拥有Web浏览器控件一个WPF页面。我有这个代码,登录用户是用PHP编写的网站:
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>
我怎么能做到这一点在WPF?至于我能理解,我需要创建一个html并将其张贴到网站。
How can I do this in wpf? As far as I can understand, I need to create an html and post it to site.
我的问题:
1 - 我怎么可以在代码中创建这样的HTML
1- How can I create such html in code?
2 - 我如何自动提交到网站(假设我是一个WPF用户控件的构造函数中做这个)。
2- How can I automatically submit it to the site (assuming I am doing this on constructor of a wpf user control).
推荐答案
据我了解,你的目标是登录并保持会话活跃 web浏览器
里面。如果是这样,你有几个选择:
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:
-
首先,浏览<$ C $ C> web浏览器到
www.asite.net
,建立会话。
然后获得<一个HREF =http://stackoverflow.com/a/18289217/1768303>底层web浏览器ActiveX控件并使用的 的IWebBrowser2 :: Navigate2
方法,它具有的PostData
参数,允许做一个HTTP POST请求。
Then obtain the underlying WebBrowser ActiveX control and use IWebBrowser2::Navigate2
method, it has PostData
parameter which allows to do an HTTP POST request.
或者注入并执行它将使用的 =http://msdn.microsoft.com/en-us/library/ie/ms535874%28v=vs.85%29的.aspx相对=nofollow> XHR 张贴的形式AJAX方法。
Or, inject and execute some JavaScript which would use XHR to post the form the AJAX way.
或者使用 web浏览器.Document
为动态
以创建一个隐藏的格式
元素,填充它,然后提交,以同样的方式你会做的JavaScript
。
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
.
或使用的 COM XMLHTTP
对象来发送POST请求,这股与会话的 web浏览器
。
您也可以使用一些低级别的URLmon API 来发送POST请求。
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");
}
}
这篇关于如何使用WPF web浏览器发布数据到Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!