我正在开发一个以windows形式出现的小程序。在这个表单上,我想要一个链接,当用户点击链接时,将打开一个单独的IE浏览器,其中包含帖子数据。
原文,我使用了System.Diagnostics.Process.start()
。但是,我不能通过这种电话发布数据。
我在网上搜索,有人建议使用“微软网页浏览器控件”,但是,这需要在我的表单中添加此控件,并在表单中显示资源管理器,但我想要一个单独的IE打开。 / p>
我在VB中看到有一个方法为CreateObject("InternetExplorer.Application")
,但我找不到如何在C#中使用它。
那么,您对如何实施有任何建议吗?
答案 0 :(得分:13)
在表单上删除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()只是传递字符串的快捷方式。
技巧是使用“_blank”作为目标框架。
答案 1 :(得分:3)
如果您在网址上使用动词OPEN执行ShellExecute,则会生成默认的网络浏览器并打开该链接。或者,您可以使用附加在字符串末尾的url调用Internet Explorer(再次使用ShellExecute)(因此用于ShellExecute的字符串将如下所示:
System.Diagnostics.Process.Start(@"C:\Program Files\Internet Explorer\iexplore.exe", "http://google.com");
你说的是POST,你不能做POST,上面的行做了GET。根据网站的设置方式,您可以将参数附加到网址的末尾,如下所示:
System.Diagnostics.Process.Start(@"C:\Program Files\Internet Explorer\iexplore.exe", "http://www.google.com/search?q=bing");
答案 2 :(得分:2)
你肯定需要使用Process.Start(或使用ProcessInfo)来启动IE:像这样:
//因为点击WinForm上的LinkLabel而将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");
}
如果您在此页面中向下滚动:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start(VS.80).aspx
(FrameWork 3.0的Process.Start文档)
您将找到一个用户提供的示例,使用ProcessInfo来控制是否启动了多个IE实例,以及如何将命令行参数传递给IE。
此页:
http://msdn.microsoft.com/en-us/library/0w4h05yb.aspx
(FrameWork 3.5的Process.Start文档)
向您展示了启动IE的完整示例,以及如何将url文件作为参数传递。
我不完全清楚你的消息中的“Post”是什么意思(我将“Post”与ASP.NET联系起来),但你可以在一个临时的位置写出一个html文件,里面你喜欢它,然后使用上面介绍的技术启动IE时传递该文件的地址。最好,
答案 3 :(得分:1)
您可以通过在Process.Start中发送URL作为参数来启动流程。由于同步上下文,从WebForms GUI线程调用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);
}
}
答案 4 :(得分:1)
您还可以使用.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对象的事件,方法和属性。
https://msdn.microsoft.com/en-us/library/aa752084(v=vs.85).aspx
我希望能帮助别人,因为它帮助了我。
我正致力于绑定事件,并会在测试后更新。
答案 5 :(得分:-3)
实际上,您可以将process.start与已发布的查询字符串数据一起使用:
System.Diagnostics.Process.Start("IExplore.exe", "http://localhost/file.html?foo=bar&baz=duh");