转:通过网卡获取http请求数据
2012-01-03 13:01 E骑士 阅读(378) 评论(0) 编辑 收藏 举报通过网卡获取http请求数据
原文地址:http://www.cnblogs.com/rq204/archive/2012/01/03/2310905.html
2012-01-03 12:33 by rq204, 6 visits, 收藏, 编辑
这个代码的原始地址是:http://www.codeproject.com/KB/cpp/sniffer80.aspx
这个是朋友反编译了一下,同时对http的解包再做了一些修改后的代码,我拿来用了。
代码调用也很简单
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Net;
9 using Sniffer;
10
11 namespace snifferHttp
12 {
13 public partial class Form1 : Form
14 {
15 public Form1()
16 {
17 InitializeComponent();
18 }
19
20 private static SnifferSocket m_Sniffer;
21 private void Form1_Load(object sender, EventArgs e)
22 {
23 m_Sniffer = new SnifferSocket();
24 m_Sniffer.TcpPacketReceived += new TcpPacketCallback(m_Sniffer_TcpPacketReceived);
25 IPAddress[] addressList = Dns.GetHostAddresses(Dns.GetHostName());
26
27 if (addressList.Length != 0)
28 {
29 foreach (IPAddress ip in addressList)
30 {
31 if (ip.ToString().Split('.').Length == 4) m_Sniffer.Sniff(ip.ToString());
32 }
33 }
34 }
35
36 void m_Sniffer_TcpPacketReceived(TcpPacket packet)
37 {
38 string data = Encoding.ASCII.GetString(packet.Data);
39 if (data.StartsWith("GET "))
40 {
41 HttpSniffer.HttpPacket sn = new HttpSniffer.HttpPacket();
42 sn.ParseRequest(data);
43
44 this.SetText("Get Request host:" + sn.Host + ",url:" + sn.URL+"cookie:"+sn.Cookie);
45 }
46 else if (data.StartsWith("HTTP/"))
47 {
48 HttpSniffer.HttpPacket sn = new HttpSniffer.HttpPacket();
49 sn.ParseRequest(data);
50 this.SetText("Get Response host:" + sn.Host + ",url:" + sn.URL + "cookie:" + sn.Cookie);
51 }
52 }
53
54 private void SetText(string text)
55 {
56 try
57 {
58 this.Invoke((EventHandler)delegate
59 {
60 this.textBox1.AppendText(text + "\r\n");
61 });
62 }
63 catch { }
64 }
65 }
66 }