WebBrowser.navigating 이벤트 핸들러에서 PostData에 액세스하려면 어떻게해야합니까?

StackOverflow https://stackoverflow.com/questions/141626

  •  02-07-2019
  •  | 
  •  

문제

Webbrowser 컨트롤이있는 .NET 3.5를 사용하여 Visual Studio 2008에 Windows 양식이 있습니다. 요청이 전송되기 전에 탐색 이벤트 핸들러에서 양식의 사후 데이터를 분석해야합니다. 그것에 도달 할 방법이 있습니까?

기존 Win32 브라우저 컨트롤에는 사후 데이터가 인수 중 하나로 사전 데이터를 갖는 이전 이벤트가있었습니다. 새로운 .NET Webbrowser 컨트롤과는 다릅니다.

도움이 되었습니까?

해결책

.NET WebBrowser 컨트롤에 의해 해당 기능이 노출되지 않습니다. 다행히도, 그 제어는 대부분 '오래된'컨트롤 주변의 래퍼입니다. 즉, 다음과 같은 것을 사용하여 알고있는 Beforenavigate2 이벤트 (?)를 구독 할 수 있습니다 (프로젝트에 shdocvw에 대한 참조를 추가 한 후).

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

... 그리고 그 이벤트 내에서 사후 데이터에 원하는 모든 것을하십시오.

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

하나의 중요한 경고 : WebBrowser.ActiveXinstance 속성에 대한 문서 "이 API는 .NET Framework 인프라를 지원하며 코드에서 직접 사용하기위한 것이 아닙니다"라고 말합니다. 다시 말해 : 예를 들어 프레임 워크 사람들이 기존 SHDOCVW com One을 래핑하는 대신 자체 브라우저 구성 요소를 구현하기로 결정한 경우와 같이, 귀하의 속성 사용은 향후 어느 시점에서나 앱을 깨뜨릴 수 있습니다.

따라서이 코드를 많은 사람들에게 배송하거나 많은 프레임 워크 버전을 위해 계속 작동 해야하는 모든 것에이 코드를 넣고 싶지 않을 것입니다 ...

다른 팁

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


C# WPF 버전

위를 유지하십시오. 그러나 교체하십시오.

    dynamic d = webBrowser1.ActiveXInstance;

와 함께:

    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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top