Search code examples
c#phpvisual-studio-2010geckofx

Visual studio 2010 c# form post data to php page using geckoWebBrowser?


i was wondering how to post data from c# form to a php using geckoWebBrowser? I have done it with IE browser on form

    public Form1()
    {
        InitializeComponent();
        String postdata = "data=somedata";

        System.Text.Encoding a = System.Text.Encoding.UTF8;

        byte[] byte1 = a.GetBytes(postdata);

        webBrowser1.Navigate("somepage.php", "", byte1, "Content-Type: application/x-www-form-urlencoded");
    }

But how to do it with geckoWebBrowser1.Navigate?


Solution

  • The method GeckoWebBrowser.Navigate() passes its parameters to a nsIWebNavigation instance, which is the XPCOM interface for running a browser instance, in this case using XULRunner 1.8 (which is pretty old).

    Unfortunately, the Navigate() method of the GeckoWebBrowser does not supply an overload for postData; it simply passes Intptr.Zero for that argument.

    I can't test it, but if you create a new method like this in GeckoWebBrowser.cs, you can call it with a string containing post data:

    public bool Navigate(string url, string postData, GeckoLoadFlags loadFlags)
    {
        if (url == null)
        {
            throw new ArgumentNullException("url");
        }
    
        if (!IsHandleCreated)
        {
                throw new InvalidOperationException("Cannot call Navigate() before the window handle is created.");
        }
    
        if (IsBusy)
        {
            this.Stop();
        }
    
        return WebNav.LoadURI(url, (uint)loadFlags, null, GetStreamFromString(postData), IntPtr.Zero) == 0;
    }
    
    public static Stream??? CreateStreamFromString(string input)
    {
        return new StreamReader???(input);
    }
    

    Please note that the LoadURI method accepts an nsIInputStream parameter, which I do not know. Try to find the parameter type and how to instantiate that, and fix the CreateStreamFromString() method to return and instantiate the right type.

    Also check out the documentation for the postData parameter:

    If the URI corresponds to a HTTP request, then this stream is appended directly to the HTTP request headers. It may be prefixed with additional HTTP headers. This stream must contain a \r\n sequence separating any HTTP headers from the HTTP request body. This parameter may be null.


    But perhaps you just want to use a WebClient or HttpClient (.NET 4.5 only) class if this is only for posting data to some URL.