用 .Net WebBrowser 控件获取POST数据

用 .Net WebBrowser 控件获取POST数据

      很多应用中,开发者需要自己构建一个浏览器,并且对通过这个浏览器提交的请求进行截获,比如用户通过这个浏览器浏览了一个网页,并向网站提交(POST) 了一些信息,我们想在其提交到网站前对这些POST数据在浏览器侧进行截获,做一些跟踪日志,或者对这些数据先加密或转换后再发送到服务器。.Net  提供的WebBrowser 控件可以让开发者很轻松的构建一个类似IE的浏览器,但这个控件提供的事件较少,一些高级应用单纯用这个控件提供的方法和事件无法实现。这篇文章我想介绍 一下如何让 WebBrowser 控件可以截获自己POST出去的数据。

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

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

      2、在 Form1_Load 中添加如下语句

SHDocVw.WebBrowser wb  =  (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance;

wb.BeforeNavigate2 += 

      new 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)
{
       string postDataText = System.Text.Encoding.ASCII.GetString(PostData as byte[]);
}

 

      完成上述3步后,你post 数据时, 就会响应 BeforeNavigate2 事件,postDataText 中就是你post的数据。你也可以修改PostData,对这些数据进行转换或加密。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Chrome based Browser Engine for .NET EO.WebBrowser is a web browser engine based on Google's Chromium project but with native .NET programming interface --- don't worry, it's not a wrapper around the Chrome browser installed on your machine. In fact EO.WebBrowser has the whole browser engine embedded inside a single .NET DLL. In another word, it has zero external dependency. Just reference and use. Why use EO.WebBrowser? • Based on Google's Chrome Project EO.WebBrowser uses the same core Google's Chrome and Apple Safari uses. It does not rely on IE. The engine is much faster and safer. • Zero External Dependency What if user updates/uninstall their browser? What if user disables JavaScript in Internet Explorer's settings dialog? These questions does not exist with EO.WebBrowser because everything is embedded inside our DLL files. • Native .NET components written in C# Because it's written in .NET, you can use it with any .NET based language/development tool. The same DLL works for both 32 bit and 64 bit environments; • Easy to use Programming Interface EO.WebBrowser offers core components that can be used in any Windows application, as well as wrapper controls for both Windows Forms applications and WPF applications; • Extensive Customization Options EO.WebBrowser offers extensive customization options that allow you to customize context menu, hot keys, JavaScript dialogs, file dialogs, focus and window control. Together these features allow you to seamlessly integrate the browser engine into your application; • .NET code -> JavaScript code Turn any web page into an integral part of your application -- both visually and programmatically. You can execute JavaScript code and access all the JavaScript objects directly from your .NET code. Access their properties or even call a JavaScript function are all different options available to you; • JavaScript code -> .NET code Things always go both ways --- and this is reflected in our programming interface as well. You can call JavaScript code from .NET code, and the other way around is also true --- you can call .NET code from your JavaScript code. This allows your Web page to seamlessly interact with the host application; • Custom Resource Handler Want to keep an eye on everything? Or want to keep everything to yourself? We got you covered. EO.WebBrowser offers ability to intercept and modify all requests that originate from the browser engine. For example, you can automatically deny all request sent to a specific host. It also offers you the ability to implement custom protocols or custom resource handlers. For example, you can implement a custom request handler to load images from your database instead of a Web server;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值