Domanda

Ho un modulo Windows in Visual Studio 2008 usando .NET 3.5 che ha un controllo WebBrowser su di esso. Devo analizzare PostData del modulo nel gestore di eventi Navigating prima di inviare la richiesta. C'è un modo per arrivarci?

Il vecchio controllo del browser win32 aveva un evento Before_Navigate che aveva PostData come uno dei suoi argomenti. Non è così con il nuovo controllo .NET WebBrowser.

È stato utile?

Soluzione

Tale funzionalità non è esposta dal controllo .NET WebBrowser. Fortunatamente, quel controllo è principalmente un involucro attorno al controllo "vecchio". Questo significa che puoi iscriverti all'evento BeforeNavigate2 che conosci e ami (?) Usando qualcosa come il seguente (dopo aver aggiunto un riferimento a SHDocVw al tuo progetto):

Dim ie = DirectCast(WebBrowser1.ActiveXInstance, SHDocVw.InternetExplorer)
AddHandler ie.BeforeNavigate2, AddressOf WebBrowser_BeforeNavigate2

... e fai quello che vuoi per PostData all'interno di quell'evento:

Private Sub WebBrowser_BeforeNavigate2(ByVal pDisp As Object, ByRef URL As Object, _
       ByRef Flags As Object, ByRef TargetFrameName As Object, _
       ByRef PostData As Object, ByRef Headers As Object, ByRef Cancel As Boolean)
    Dim PostDataText = System.Text.Encoding.ASCII.GetString(PostData)
End Sub

Un avvertimento importante: la per La proprietà WebBrowser.ActiveXInstance afferma che " Questa API supporta l'infrastruttura .NET Framework e non deve essere utilizzata direttamente dal tuo codice. & Quot ;. In altre parole: l'utilizzo della proprietà potrebbe interrompere la tua app in qualsiasi momento in futuro, ad esempio quando le persone del Framework decideranno di implementare il proprio componente browser, invece di racchiudere quello esistente SHDocVw COM.

Quindi, non vorrai inserire questo codice in qualsiasi cosa spedisci a molte persone e / o in qualsiasi cosa che dovrebbe rimanere funzionante per molte versioni di Framework in arrivo ...

Altri suggerimenti

Versione C #

    /// <summary>
    /// Fires before navigation occurs in the given object (on either a window or frameset element).
    /// </summary>
    /// <param name="pDisp">Object that evaluates to the top level or frame WebBrowser object corresponding to the navigation.</param>
    /// <param name="url">String expression that evaluates to the URL to which the browser is navigating.</param>
    /// <param name="Flags">Reserved. Set to zero.</param>
    /// <param name="TargetFrameName">String expression that evaluates to the name of the frame in which the resource will be displayed, or Null if no named frame is targeted for the resource.</param>
    /// <param name="PostData">Data to send to the server if the HTTP POST transaction is being used.</param>
    /// <param name="Headers">Value that specifies the additional HTTP headers to send to the server (HTTP URLs only). The headers can specify such things as the action required of the server, the type of data being passed to the server, or a status code.</param>
    /// <param name="Cancel">Boolean value that the container can set to True to cancel the navigation operation, or to False to allow it to proceed.</param>
    private delegate void BeforeNavigate2(object pDisp, ref dynamic url, ref dynamic Flags, ref dynamic TargetFrameName, ref dynamic PostData, ref dynamic Headers, ref bool Cancel);

    private void Form1_Load(object sender, EventArgs e)
    {
        dynamic d = webBrowser1.ActiveXInstance;

        d.BeforeNavigate2 += new BeforeNavigate2((object pDisp,
            ref dynamic url,
            ref dynamic Flags,
            ref dynamic TargetFrameName,
            ref dynamic PostData,
            ref dynamic Headers,
            ref bool Cancel) => {

            // Do something with PostData
        });
    }

Versione WPF C #

Conservare quanto sopra, ma sostituire:

    dynamic d = webBrowser1.ActiveXInstance;

con:

    using System.Reflection;
    ...
    PropertyInfo prop = typeof(System.Windows.Controls.WebBrowser).GetProperty("ActiveXInstance", BindingFlags.NonPublic | BindingFlags.Instance);
    MethodInfo getter = prop.GetGetMethod(true);
    dynamic d = getter.Invoke(webBrowser1, null);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top