2008-09-26 3 views
4

WebBrowser 컨트롤이있는 .NET 3.5를 사용하여 Visual Studio 2008에서 Windows 양식을 얻었습니다. 요청을 보내기 전에 Navigating 이벤트 처리기에서 양식의 PostData를 분석해야합니다. 그것에 도달 할 수있는 방법이 있습니까?WebBrowser.Navigating 이벤트 처리기에서 PostData에 어떻게 액세스합니까?

이전 Win32 브라우저 컨트롤에는 PostData가 인수 중 하나 인 Before_Navigate 이벤트가있었습니다. 새로운 .NET WebBrowser 컨트롤에서는 그렇지 않습니다.

답변

6

그 기능은 .NET WebBrowser 컨트롤에 의해 노출되지 않습니다. 다행히도, 그 컨트롤은 대부분 '오래된'컨트롤을 둘러싼 래퍼입니다. (?)이 (프로젝트에 SHDOCVW에 대한 참조를 추가 한 후) 다음과 같은 것을 사용하면 BeforeNavigate2의 당신이 알고있는 이벤트와 사랑에 가입 할 수 있습니다 의미 :

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

을 ... 그리고 당신이 원하는 무엇이든 할 해당 이벤트 안쪽과 PostData :

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 

한 가지 중요한주의하십시오 documentation for the WebBrowser.ActiveXInstance property이 ".이 API는 .NET Framework 인프라를 지원하며 사용자 코드에서 직접 사용할 수 없습니다"고 주장한다. 즉, 예를 들어 프레임 워크 사용자가 기존 SHDocVw COM을 래핑하는 대신 자신의 브라우저 구성 요소를 구현하기로 결정한 경우와 같이 자산을 사용하면 향후 언제든지 앱이 중단 될 수 있습니다.

그래서, 당신은 당신이 사람과/또는 온 많은 프레임 워크 버전의 작업 남아 있어야 무엇이든 많이로 배송 아무것도에서이 코드 ...

+0

고맙습니다. 나는 이것이 대답 일지 모른다는 것을 두려워했다. 새로운 WebBrowser로 이동하여 더 이상 두려운 Shdocvw를 참조하지 않아도됩니다. 따라서 정답 일지라도 이것은 실제로 우리에게 도움이되지 않습니다. –

7

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 some with PostData 
     }); 
    } 
관련 문제