Use the. NET webbrowser control to obtain post data
In many applications, developers need to build a browser and intercept requests submitted through the browser. For example, a user browses a webpage through the browser and submits a post to the website) we want to intercept the post data on the browser side before it is submitted to the website, and make some tracking logs, or encrypt or convert the data before sending it to the server .. The webbrowser control provided by. Net allows developers to easily build a browser similar to IE, but this control provides fewer events, some advanced applications simply use the methods and events provided by this control. This article will introduce how to enable the webbrowser control to intercept post data.
Webbrowser is actually an encapsulation of the ActiveX control shdocvw, and many underlying calls of this shdocvw webbrowser control are not provided for implementation. We need to directly operate the shdocevw control to implement these advanced calls. The procedure is as follows:
1. Find the dynamic library shdocvw. dll in the Windows/system32 directory and add it to the reference.
2. Add the following statement to form1_load:
Shdocvw. webbrowser WB = (shdocvw. webbrowser) webbrowser1.activexinstance;
WB. beforenavigate2 + =
New dwebbrowserevents2_beforenavigate2eventhandler (webbrowser_beforenavigate2 );
3. Add the following member functions
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 []);
}
After completing the preceding three steps, when you post data, you will respond to the beforenavigate2 event. postdatatext contains your post data. You can also modify postdata to convert or encrypt the data.