Post 如何在C#中打开带有帖子信息的IE?
我正在开发一个作为windows窗体的小程序。在这个表单上,我想放置一个链接,当用户单击该链接时,一个单独的IE浏览器将打开,其中包含帖子数据 最初,我使用了Post 如何在C#中打开带有帖子信息的IE?,post,internet-explorer,Post,Internet Explorer,我正在开发一个作为windows窗体的小程序。在这个表单上,我想放置一个链接,当用户单击该链接时,一个单独的IE浏览器将打开,其中包含帖子数据 最初,我使用了System.Diagnostics.Process.start()。然而,我不能通过这种电话发布数据 我搜索了网页,有人建议使用“MicrosoftWebBrowser控件”,然而,这需要在我的表单中添加此控件,并在表单中显示浏览器,但我希望单独打开IE 我在VB中看到了一个名为CreateObject(“InternetExplorer
System.Diagnostics.Process.start()
。然而,我不能通过这种电话发布数据
我搜索了网页,有人建议使用“MicrosoftWebBrowser控件”,然而,这需要在我的表单中添加此控件,并在表单中显示浏览器,但我希望单独打开IE
我在VB中看到了一个名为CreateObject(“InternetExplorer.Application”)
的方法,但我找不到如何在C#中使用它
那么,您对如何实现有什么建议吗?在表单上放置一个web浏览器。它应该有一个默认名称“webBrowser1”-如果您愿意,您可以更改它。将“Visible”属性设置为“False”。双击表单标题栏以在代码中自动生成加载事件 调用具有以下签名的Navigate方法:
void Navigate(string urlString, string targetFrameName, byte[] postData, string additionalHeaders);
像这样:
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.google.com/", "_blank", Encoding.Default.GetBytes("THIS IS SOME POST DATA"), "");
}
你可以在那里传递任何你想要的字节数组。。。Encoding.Default.GetBytes()只是传递字符串的一种快速方法
诀窍是对目标帧使用“\u blank”。实际上,您可以使用process.start,从发布的查询字符串数据开始:
System.Diagnostics.Process.Start("IExplore.exe", "http://localhost/file.html?foo=bar&baz=duh");
如果在url上使用动词OPEN执行ShellExecute,则会生成默认web浏览器并打开链接。或者,您可以调用Internet Explorer(再次使用ShellExecute),并在字符串末尾附加url(因此用于ShellExecute的字符串如下所示:
System.Diagnostics.Process.Start(@"C:\Program Files\Internet Explorer\iexplore.exe", "http://google.com");
虽然你说的是POST,但你不能做POST,上面的一行是GET。根据网站的设置方式,你可以只在url的末尾附加参数,如下所示:
System.Diagnostics.Process.Start(@"C:\Program Files\Internet Explorer\iexplore.exe", "http://www.google.com/search?q=bing");
您肯定需要使用Process.Start(或使用ProcessInfo)来启动IE:如下所示: //单击WinForm上的链接标签后,打开IE至桌面上的文件
internal static string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("IExplore.exe", desktopPath + "\\someHTMLFile.htm");
}
如果在此页面中向下滚动:
(框架3.0的流程启动文档)
您将看到一个用户参与的示例,该示例使用ProcessInfo控制是否启动了多个IE实例,以及如何将命令行参数传递给IE
本页:
(流程:启动框架3.5的文档)
向您展示了启动IE的完整示例,以及如何将url文件作为参数传递
我不完全清楚你的信息中“Post”是什么意思(我把“Post”与ASP.NET联系在一起),但您可以在临时位置写出一个html文件,其中包含您喜欢的内容,然后在启动IE时使用上述技术传递该文件的地址。最好是,您可以通过在进程中发送URL来启动进程。以参数形式启动。从WebForms GUI线程be调用StartProcess时出现问题同步上下文的原因。我的解决方案为此使用线程池。此解决方案的优点是在用户首选的web浏览器中打开URL,该浏览器可以是IE、Firefox等
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming",
"CA1725:ParameterNamesShouldMatchBaseDeclaration", MessageId = "0#"),
System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
public void OpenUrl(string urlString)
{
try
{
ThreadPool.QueueUserWorkItem(delegate { StartProcess(urlString, null); });
}
catch (Exception ex)
{
log.Error("Exception during opening Url (thread staring): ", ex);
//do nothing
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
public void StartProcess(string processName, string arguments)
{
try
{
Process process = new Process();
process.StartInfo.FileName = processName;
if (!string.IsNullOrEmpty(arguments))
{
process.StartInfo.Arguments = arguments;
}
process.StartInfo.CreateNoWindow = false;
process.StartInfo.UseShellExecute = true;
process.Start();
}
catch (Exception ex)
{
log.ErrorFormat("Exception in StartProcess: process: [{0}], argument:[{1}], exception:{2}"
, processName, arguments, ex);
}
}
您还可以使用.NET反射打开浏览器 此示例演示如何设置InternetExplorer.Application的某些特定属性 例如,我需要能够关闭地址栏并设置高度和宽度。在其他示例中,IE和其他浏览器安全性不允许您关闭地址栏 我们的网站是一个内部MVC应用程序,没有任何问题
System.Type oType = System.Type.GetTypeFromProgID("InternetExplorer.Application");
object IE = System.Activator.CreateInstance(oType);
IE.GetType().InvokeMember("menubar", System.Reflection.BindingFlags.SetProperty, null, IE, new object[] { 0 });
IE.GetType().InvokeMember("toolbar", System.Reflection.BindingFlags.SetProperty, null, IE, new object[] { 0 });
IE.GetType().InvokeMember("statusBar", System.Reflection.BindingFlags.SetProperty, null, IE, new object[] { 0 });
IE.GetType().InvokeMember("addressbar", System.Reflection.BindingFlags.SetProperty, null, IE, new object[] { 0 });
IE.GetType().InvokeMember("Visible", System.Reflection.BindingFlags.SetProperty, null, IE, new object[] { true });
IE.GetType().InvokeMember("Height", System.Reflection.BindingFlags.SetProperty, null, IE, new object[] { 680 });
IE.GetType().InvokeMember("Width", System.Reflection.BindingFlags.SetProperty, null, IE, new object[] { 1030 });
IE.GetType().InvokeMember("Navigate", System.Reflection.BindingFlags.InvokeMethod, null, IE, new object[] { "http://yoursite" });
这里唯一的缺点是,这是专门打开IE。另外,它让你对浏览器有更多的控制
您还可以访问InternetExplorer.Application对象的事件、方法和属性
我希望这能像帮助我一样帮助别人
我正在处理与事件的绑定,并将在测试后进行更新。您只需使用Webbrowser控件的Navigate()方法,并将其完全隐藏在表单中即可。这将执行GET,而不是POST,您可以说嵌入控件只是为了执行流程。Start()无论如何都可以吗?这实际上是.NET最好的简单答案。更“正确”的答案包括在“InternetExplorer.Application”上调用CoCreateInstanceprogID,在您完成CCI后,您可以调用.Navigate2接口并传递post数据。但这将涉及一堆COM/PInvoke goo,这一答案巧妙地避免了。是否有随机的向下投票人在不留下解释的情况下向下投票?通过“post”他指的是在HTTP中向服务器提交数据的POST方法。请参见此处的第9.5节:。yongmei的问题是Process.Start(url)方法使用GET方法(此处的第9.3节),这是一种不太安全的方法。