c# webBrowser 获取 post 返回的数据

c# 如何 获取post 返回的数据 (部分未显示) post返回的是一个表格 但表格内容没有完全显示

img

可能对你有用的两个办法:
办法一:

办法二:

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

或者下面的方法:

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

webbrowser控件,是微软windows内置的IE内核,众所周知,微软自己都已经放弃这个内核了。
目前做爬虫的没人用webbrowser了。
我在用谷歌内核,cef。
或者,你实在想用webbrowser,并且你的场景只是要截取post请求,那可以看看Fiddler Core。

WebBrowser 其实是对 ActiveX 控件 SHDocVw 的封装,而这个SHDocVw的很多底层调用WebBrowser控件并没有提供实现,我们需要直接操作 SHDoceVw 控件来实现这些高级调用。

  1. 在 windows/system32 目录下找到 shdocvw.dll 这个动态库,将其添加到你的项目引用中;

  2. 在窗体加载事件(Load) 中添加以下代码;

    private void Form1_Load(object sender, EventArgs e)
            {
              webBrowser1.Url = new Uri("https://localhost:44323/IndexPost.html");
    
              SHDocVw.WebBrowser wb = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance;
              wb.BeforeNavigate2 += new SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(WebBrowser_BeforeNavigate2);
          }
    
  3. 添加如下成员函数;

    private void WebBrowser_BeforeNavigate2(object pDisp, ref object URL, ref object Flags,
    ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
          {
              try
              {
                  string postDataText = System.Text.Encoding.ASCII.GetString(PostData as byte[]);
              }
              catch (Exception)
              {
              }
          }
    

    好了完成上述3步后,以上就是项目中需要添加的代码,你post 数据时, 就会响应 BeforeNavigate2 事件,postDataText 中就是你post的数据。

img

img

需要源码的话,我可以发给你。

最好使用HttpWebRequest和HttpWebResponse类,来获取Post返回数据

我感觉你是想去解析HTML,所以建议使用:HtmlAgilityPack