Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open IE with post info in C#?

I am developing a small program which as a windows form. On this form, I want to put a link, and when user click the link, a seperate IE browser will be open with post data.

Original, i used System.Diagnostics.Process.start(). However, I can not post data through this kind of call.

And I searched the web, some guy suggested to use "Microsoft web browser control", however, this need to add this control in my form,and display the explorer in the form, but I want a seperate IE opened.

And I saw in VB there is a method as CreateObject("InternetExplorer.Application"), but I can not find how to use it in C#.

So, do you have any suggestions on how to implement?

like image 695
yongmei Avatar asked Nov 10 '09 01:11

yongmei


3 Answers

Drop a web browser on your form. It should have a default name of "webBrowser1" - you can change that if you like. Set the "Visible" property to "False". Double-click the form title bar to auto generate a load event in the code.

Call the Navigate method, which has this signature:

void Navigate(string urlString, string targetFrameName, byte[] postData, string additionalHeaders);

Like this:

private void Form1_Load(object sender, EventArgs e)
{
    webBrowser1.Navigate("http://www.google.com/", "_blank", Encoding.Default.GetBytes("THIS IS SOME POST DATA"), "");
}

You can pass any array of bytes you want in there... Encoding.Default.GetBytes() is just a fast way to pass a string through.

The trick is to use "_blank" for the target frame.

like image 63
Andy S Avatar answered Dec 09 '22 19:12

Andy S


If you do a ShellExecute with a verb of OPEN on the url then the default web browser will be spawned and open the link. Alternatively, you can invoke Internet Explorer (once again using ShellExecute) with the url appended at the end of the string (so the string that you use for ShellExecute would look like this:

System.Diagnostics.Process.Start(@"C:\Program Files\Internet Explorer\iexplore.exe", "http://google.com");

You are talking about POST though, you cannot do a POST, the above line does a GET. Depending on how the website is set up you may be able to just append the parameters on the end of the url, like so:

System.Diagnostics.Process.Start(@"C:\Program Files\Internet Explorer\iexplore.exe", "http://www.google.com/search?q=bing");
like image 37
slugster Avatar answered Dec 09 '22 20:12

slugster


You are definitely going to need to use Process.Start (or use a ProcessInfo) to get IE started : like this :

// open IE to a file on the Desktop as a result of clicking a LinkLabel on a WinForm

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

If you scroll down in this page :

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start(VS.80).aspx

(the Process.Start docs for FrameWork 3.0)

you'll find a user contributed example of using ProcessInfo to control whether more than one of instance of IE is started, and how to pass command line arguments to IE.

This page :

http://msdn.microsoft.com/en-us/library/0w4h05yb.aspx

(the Process.Start docs for FrameWork 3.5)

shows you a complete example of launching IE, and how to pass url files as arguments.

I'm not completely clear on what you mean by "Post" in your message (I associate "Post" with ASP.NET), but you could write out an html file in a temporary location with whatever you liked in it, and then pass the address of that file when you launch IE using the techniques documented above. best,

like image 44
BillW Avatar answered Dec 09 '22 18:12

BillW