gpt4 book ai didi

c# - IWebBrowserApp.Navigate() 如何发送 Post 数据

转载 作者:太空狗 更新时间:2023-10-30 00:28:21 25 4
gpt4 key购买 nike

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

考虑例如。

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

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

编辑

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

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

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

非常感谢您的帮助。

最佳答案

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

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

  • 然后用 & 分隔的字段及其值创建 post string,然后将该 string 转换为 字节数组.

  • 最后只需要请求IE导航到url,同时发送转换成字节数组的Post Data,然后添加我们提交表单时添加的相同标题。

这里是:

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

注意:

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

PHP 代码示例:

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

ASP 代码示例:

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

页面会显示如下内容:

fabio 123

关于c# - IWebBrowserApp.Navigate() 如何发送 Post 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3348930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com