如何使用 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
How to post data to a web server using wpf webbrowser
提问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. 如果是这样,您有几个选择:
First, navigate the
WebBrowsertowww.asite.net, to establish the session.Then obtain the underlying WebBrowser ActiveX controland use
IWebBrowser2::Navigate2method, it hasPostDataparameter which allows to do an HTTP POST request.Or, inject and execute some JavaScriptwhich would use XHRto post the form the AJAX way.
Or, use
WebBrowser.Documentasdynamicto create a hiddenformelement, populate it and submit it, in the same way you'd do withJavaScript.Or, use COM
XMLHTTPobjectto send a POST request, it shares the session with theWebBrowser.You could also use some low level UrlMon APIto send a POST request.
首先,导航
WebBrowser到www.asite.net,以建立会话。然后获取底层的WebBrowser ActiveX 控件和使用
IWebBrowser2::Navigate2方法,它具有PostData允许进行HTTP POST 请求的参数。或者,注入并执行一些将使用XHR以 AJAX 方式发布表单的JavaScript。
或者,使用
WebBrowser.Documentasdynamic创建一个隐藏form元素,填充它并提交它,就像使用JavaScript.或者,使用COM
XMLHTTP对象发送 POST 请求,它与WebBrowser.您还可以使用一些低级 UrlMon API来发送 POST 请求。
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命名空间,尤其是WebRequest和WebResponse对象。
See this previous answer, it should get you started:
看到这个以前的答案,它应该让你开始:

