package test2;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
import jpcap.packet.Packet;
import jpcap.packet.TCPPacket;
public class HTTPPacketTest {
public static void main (String [] args) throws IOException {
/ * ------------- Step bind network equipment -------------------- * /
NetworkInterface [] devices = JpcapCaptor.getDeviceList (); / / returns a list of network devices
for (NetworkInterface n: devices)
System.out.println (n.name + "|" + n.description ;) ;
System.out.println ("------------------------------------" ) ;
JpcapCaptor jpcap = null;
int caplen = 1512;
boolean promiscCheck = true;
/ / caplen limit every time it receives a data packet , extracts only the number of bytes in the packet before
/ / Promisc: Set whether promiscuous mode. In promiscuous mode will receive all data packets, if later called packet filtering function setFilter () will have no effect
/ / 50 This parameter is mainly used processPacket () method, specifying the timeout
try {
jpcap = JpcapCaptor.openDevice (devices [0], caplen, promiscCheck, 50);
} catch (IOException e) {
e.printStackTrace ();
}
/ * ---------------- Step Ethereal --------------- * /
int i = 0;
String s = null;
String result = null;
while (i <100)
{
Packet packet = jpcap.getPacket ();
if (packet instanceof TCPPacket)
{
TCPPacket tcpPacket = (TCPPacket) packet;
if (tcpPacket.src_port == 80 | | tcpPacket.dst_port == 80) / / define port 80 , identified as HTTP packets
{
i + +;
System.out.print (" data : \ n");
for (i = 0; i
}
try
{
File file = new File ("D: \ \ about.txt");
if (! file.exists ()) {
file.createNewFile ();
}
/ / byte [] b = result.getBytes ();
/ / FileOutputStream fout = new FileOutputStream (myFile);
RandomAccessFile raf = new RandomAccessFile (file, "rw"); / / save the file in Notepad
raf.seek (raf.length ());
if (raf.read ()! = -1) {
raf.writeByte (tcpPacket.data [i]);
}
while (raf.read ()! = -1) {
s = raf.readLine ();
System.out.println (s);
/ / regular expression matching , grab the url of the document
Pattern p = Pattern.compile ("Referer: (. *?) (html)");
Matcher m = p.matcher (s);
if (m.find ()) {
result = m.group (1) + m.group (2);
}
System.out.println (" result set output :" + result);
/ * Pattern p = Pattern.compile ("Referer: (. *?) Accept-Encoding:");
Matcher m = p.matcher (string);
if (m.find ()) {
System.out.println (" Regular Output :" + m.group (1));
} * /
raf.close ();
}
}
catch (FileNotFoundException e)
{
System.err.println (e);
}
catch (IOException e)
{
System.err.println (e);
} / / exception handling
}
}
}
}
}
run it can be found that is simply not the regular expression matching output checked, it should be coding problem , ask a favor predecessors have done , to help give a way out ah ! ! !
------ Solution ---------------------------------------- ----
you are now the problem is not accurate access to the data packet coding, data analysis garbled. To get to the packet 's natural code , or can not know the data is encoded .
------ For reference only -------------------------------------- -
How to obtain accurate data packet encoding and parsing ? Brother can guide it , tangled for a long time here . . . Thank you
------ For reference only ------------------------------------ ---
constantly brush ah, ah solving , seeking god help ah ~ ~ ~
------ For reference only ----------------- ----------------------
handle this problem yet ? I also want to get URL
------ For reference only -------------------------------- -------
same question , the landlord of the problem solved?
没有评论:
发表评论