views:

603

answers:

5

Hi, 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 csharp.

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

+3  A: 

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.

Andy S
I doubt that you could say it is efficient to embed a control just to do what Process.Start() can do anyway?
slugster
This is effectively the best simple answer for .NET.The more "correct" answer involves calling CoCreateInstance on the "InternetExplorer.Application" progID, and after you CCI, you can call the .Navigate2 interface and pass the post data. But that's going to involve a bunch of COM/PInvoke goo that this answer neatly avoids.
EricLaw -MSFT-
A: 

Actually, you can use process.start with posted query string data:

System.Diagnostics.Process.Start("IExplore.exe", "http://localhost/file.html?foo=bar&baz=duh");
ryber
That performs a GET, not a POST
Andy S
A: 

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");
slugster
Is there are random downvoter going around down voting things without leaving an explanation?
slugster
up-voting to counter non commenting random downvoter
ryber
A: 

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%28VS.80%29.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,

BillW
By "post" he means the POST method to submit data to a server, in HTTP. See section 9.5 here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html.yongmei's issue is that the Process.Start(url) way of doing things uses the GET method (section 9.3 there) which is less secure.
urig
A: 

You can just start process by sending URL in Process.Start as parameter. There is a problem while calling StartProcess from WebForms GUI thread because of synchronization context. My solution uses thread pool for this purpose. Advantage of this solution is that an URL is opened in user preferred web browser that can be IE, Firefox etc.

[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);
  }
}
Andrej Golcov