【问题标题】:IWebBrowserApp.Navigate() How to send Post DataIWebBrowserApp.Navigate() 如何发送 Post 数据
【发布时间】:2011-03-21 21:45:51
【问题描述】:

如果有人可以向我展示如何使用导航方法发送 POST 数据的好示例,我将非常高兴,可通过SHDocVw.IWebBrowserApp 获得。

例如考虑。

我们应该去的页面是:http://example.com/check.php

并且应该发送两个输入字段的值:用户名和密码。

编辑

我正在尝试使用我的 C# 应用程序使用 Windows 操作系统上可用的本机 Internet Explorer 版本 7 或更高版本,向特定 URL 发送 HTTP 请求,使用 POST 方法将用户的用户名和密码传递给处理 HTTP 响应的服务器端页面。

使用 IWebBrowserAppNavigate 方法,我可以打开 Internet Explorer 的新窗口/实例并将其发送到特定页面(本地或网络中),如果指定还发送 POST 数据和自定义标题。

但主要问题是我不知道如何将我的数据写入浏览器携带的 POST 请求中。

我将不胜感激。

【问题讨论】:

  • ábio:没有理由在标题和标签中都添加“C#”。请把它留在标签中。
  • @Jonh Saunders:当然,你对我的问题有什么建议吗?

标签: c# shdocvw


【解决方案1】:

我找到了如何命令 IE 打开网页并发送一些 POST 数据。

  • 将名为 Microsoft Internet Explorer Controls 的 COM 引用添加到项目中。

  • 然后创建post string,其字段及其值由&分隔,然后将string转换为byte array

  • 最后只需要请求IE导航到url,并将Post Data转换为byte array,然后添加与提交表单时相同的Header .

这里是:

using SHDocVw; // Don't forget

InternetExplorer IEControl = new InternetExplorer();
IWebBrowserApp IE = (IWebBrowserApp)IEControl;
IE.Visible = true;

// Convert the string into a byte array
ASCIIEncoding Encode = new ASCIIEncoding();
byte[] post = Encode.GetBytes("username=fabio&password=123");

// The destination url
string url = "http://example.com/check.php";

// The same Header that its sent when you submit a form.
string PostHeaders = "Content-Type: application/x-www-form-urlencoded";

IE.Navigate(url, null, null, post, PostHeaders);

注意:

试试这是否可行。不要忘记您的服务器端页面必须写入/回显 Post 字段名为:用户名和密码。

PHP 代码示例:

<?php
echo $_POST['username'];
echo " ";
echo $_POST['password'];
?>

ASP 代码示例:

<%
response.write(Request.Form("username"))
response.write(" " & Request.Form("password"))
%>

页面会显示如下:

法比奥 123

【讨论】:

    猜你喜欢
    • 2011-04-19
    • 2016-04-25
    • 2022-01-03
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 2019-06-23
    相关资源
    最近更新 更多