监听webbrowser所有请求(含 js/css/flash/ajax 等请求) 附完整源码

【实例简介】

IE11下不可用,请在低版本IE测试

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

文件:590m.com/f/25127180-488083200-474e7a(访问密码:551685)

【实例截图】

监听webbrowser所有请求(含 js/css/flash/ajax 等请求) 附完整源码
【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

using System.Runtime.InteropServices ;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private class CustomTag
{
public string RequestHeaders { get; set; }
public string ResponseHeaders { get; set; }
public byte[] RequestBody { get; set; }
};

    public Form1()
    {
        InitializeComponent();
    }

    private void OnRequestCompleted(string verb
        , string url
        , string requestHeaders
        , string responseHeaders
        , string requestBodyFilename
        )
    {
        byte[] buffer = new byte[0];
        if (!string.IsNullOrEmpty(requestBodyFilename))
        {
            using (FileStream fs = new FileStream(requestBodyFilename, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite))
            {
                buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fs.Close();
            }
        }
         
        var lvi = new ListViewItem() { Text = DateTime.Now.ToString() };
        lvi.SubItems.Add(verb);
        lvi.SubItems.Add(url);
        lvi.Tag = new CustomTag 
        {
            RequestHeaders = requestHeaders,
            ResponseHeaders = responseHeaders,
            RequestBody = buffer
        };
        HandleRequestDelegate del = this.HandleRequest;
        this.listView1.BeginInvoke(del, lvi);
    }

    private delegate void HandleRequestDelegate(ListViewItem lvi);

    private void HandleRequest(ListViewItem lvi)
    {
        this.listView1.Items.Add(lvi);
    }


    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.Navigate("http://www.ifeng.com");

        this.listView1.SelectedIndexChanged  = new EventHandler(listView1_SelectedIndexChanged);

        // initialize
        WininetHookWrapper.SetRequestCompletionCallback(this.OnRequestCompleted);
    }

    void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listView1.SelectedItems.Count == 0)
            return;

        var tag = (listView1.SelectedItems[0].Tag as CustomTag);
        txtRequestHeaders.Text = tag.RequestHeaders;
        txtResponseHeaders.Text = tag.ResponseHeaders;
        txtBody.Text = Encoding.ASCII.GetString(tag.RequestBody);
    }
}

}

上一篇:使用weixin-java-open请求微信第三方平台接口超时


下一篇:C#_发邮件