监听webbrowser所有请求(含 js/css/flash/ajax 等请求)

发布时间2019/7/4 9:06:46

下载次数 0 次 访问量87 次

免开发-应用中心上线啦,专注提供优质插件源码和产品应用!

监听webbrowser的所有请求以及查看 请求的详细,含session cookie 请求标题等信息

其中参考了如下项目:

http://www.codeproject.com/Articles/38616/Retrieve-HttpOnly-Session-Cookie-in-WebBrowser


【实例截图】


usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.IO;
usingSystem.Windows.Forms;
 
usingSystem.Runtime.InteropServices ;
 
namespaceWindowsFormsApplication1
{
    publicpartialclassForm1 : Form
    {
        privateclassCustomTag
        {
            publicstringRequestHeaders {get;set; }
            publicstringResponseHeaders  {get;set; }
            publicbyte[] RequestBody {get;set; }
        };
 
 
        publicForm1()
        {
            InitializeComponent();
        }
 
        privatevoidOnRequestCompleted(stringverb
            ,stringurl
            ,stringrequestHeaders
            ,stringresponseHeaders
            ,stringrequestBodyFilename
            )
        {
            byte[] buffer =newbyte[0];
            if(!string.IsNullOrEmpty(requestBodyFilename))
            {
                using(FileStream fs =newFileStream(requestBodyFilename, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite))
                {
                    buffer =newbyte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    fs.Close();
                }
            }
             
            var lvi =newListViewItem() { Text = DateTime.Now.ToString() };
            lvi.SubItems.Add(verb);
            lvi.SubItems.Add(url);
            lvi.Tag =newCustomTag
            {
                RequestHeaders = requestHeaders,
                ResponseHeaders = responseHeaders,
                RequestBody = buffer
            };
            HandleRequestDelegate del =this.HandleRequest;
            this.listView1.BeginInvoke(del, lvi);
        }
 
        privatedelegatevoidHandleRequestDelegate(ListViewItem lvi);
 
        privatevoidHandleRequest(ListViewItem lvi)
        {
            this.listView1.Items.Add(lvi);
        }
 
 
        privatevoidForm1_Load(objectsender, EventArgs e)
        {
            webBrowser1.Navigate("http://www.ifeng.com");
 
            this.listView1.SelectedIndexChanged  =newEventHandler(listView1_SelectedIndexChanged);
 
            // initialize
            WininetHookWrapper.SetRequestCompletionCallback(this.OnRequestCompleted);
        }
 
        voidlistView1_SelectedIndexChanged(objectsender, EventArgs e)
        {
            if(listView1.SelectedItems.Count == 0)
                return;
 
            var tag = (listView1.SelectedItems[0].TagasCustomTag);
            txtRequestHeaders.Text = tag.RequestHeaders;
            txtResponseHeaders.Text = tag.ResponseHeaders;
            txtBody.Text = Encoding.ASCII.GetString(tag.RequestBody);
        }
    }
}

网友评论

发表评论

欢迎评论哦!