gpt4 book ai didi

c# - Web 浏览器控件在调用表单提交方法时在 IE 中打开下一页

转载 作者:太空宇宙 更新时间:2023-11-03 10:52:27 25 4
gpt4 key购买 nike

我正在尝试创建自动表单提交应用程序。我需要订阅不同的网站。它涉及提交两种形式。我正在使用 .Net 网络浏览器控件。当我填写第一个表单并调用表单的提交方法或按钮的单击方法时,都会在 IE 中打开下一页。 (在窗口浏览器中,第一个表单在点击提交按钮时在新选项卡中打开下一个表单)

HtmlElementCollection inputs = form.GetElementsByTagName("input");
foreach (HtmlElement input in inputs)
{
if (input.GetAttribute("type") == "text")
{
inputname = input.GetAttribute("name");
input.SetAttribute("value", email);
}

else if (input.GetAttribute("type") == "submit")
{
//input.InvokeMember("click");
form.InvokeMember("submit");
break;
}
}

所以问题是它不会在控件中打开下一个表单,而是在 IE 中打开它。我怎样才能让它在控件中打开下一个表单。

如有任何帮助,我们将不胜感激。谢谢

最佳答案

以下对我有用。按原样尝试,找出与您的代码有何不同。

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinformsWb
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}

const string HTML_DATA =
"<form target='_blank' action='http://requestb.in/yc89ojyc' method='post' enctype='multipart/form-data'>" +
"<input type='hidden' name='arg' value='val'>" +
"<button type='submit' name='go' value='submit'>Submit</button>" +
"</form>";

// this code depends on SHDocVw.dll COM interop assembly,
// generate SHDocVw.dll: "tlbimp.exe ieframe.dll",
// and add as a reference to the project

private async void MainForm_Load(object sender, EventArgs e)
{
// make sure the ActiveX has been created
if ((this.webBrowser.Document == null && this.webBrowser.ActiveXInstance == null))
throw new ApplicationException("webBrowser");

// handle NewWindow
var activex = (SHDocVw.WebBrowser_V1)this.webBrowser.ActiveXInstance;
activex.NewWindow += delegate(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
{
Processed = true;

object flags = Flags;
object targetFrame = Type.Missing;
object postData = PostData != null ? PostData : Type.Missing;
object headers = !String.IsNullOrEmpty(Headers) ? Headers.ToString() : Type.Missing;

SynchronizationContext.Current.Post(delegate
{
activex.Navigate(URL, ref flags, ref targetFrame, ref postData, ref headers);
}, null);
};

// navigate to a stream
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))
{
writer.Write(HTML_DATA);
writer.Flush();
stream.Position = 0;

// naviage and await DocumentCompleted
var tcs = new TaskCompletionSource<bool>();
WebBrowserDocumentCompletedEventHandler handler = (s, arg) =>
tcs.TrySetResult(true);
this.webBrowser.DocumentCompleted += handler;
this.webBrowser.DocumentStream = stream;
await tcs.Task;
this.webBrowser.DocumentCompleted -= handler;
}

// click the button
var button = this.webBrowser.Document.GetElementById("go");
button.InvokeMember("click");
}
}
}

关于c# - Web 浏览器控件在调用表单提交方法时在 IE 中打开下一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20934420/

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