Java uses grab packets to get the external request sent by the application

Source: Internet
Author: User
Tags http request

Java uses the Jpcap grab tool to obtain all TCP requests sent by this machine, and can debug the relevant HTTP request or SQL connection request sent by an application after startup according to the content of the TCP request, so that we can debug our program very well. It's good. An application without intrusive debugging the native sent HTTP interface, SQL connection interface and so on.

Import java.io.UnsupportedEncodingException;

Import java.net.InetAddress;
Import jpcap.*;
Import Jpcap.packet.EthernetPacket;
Import Jpcap.packet.Packet;

Import Jpcap.packet.TCPPacket;

		Class Tcpdump implements Packetreceiver {public void Receivepacket (Packet Packet) {//System.out.println (Packet);
			if (packet instanceof tcppacket) {tcppacket tcppacket = (tcppacket) packet; if (TcpPacket.src_ip.getHostAddress (). Equals (GetIP ()) {Ethernetpacket Ethernetpacket = (ethernetpacket)
				Packet.datalink; System.out.print ("Source IP:" + tcppacket.src_ip + "Destination IP:" + tcppacket.dst_ip + "send port:" + Tcppacket.src_port + "Then
				Receiving port: "+ tcppacket.dst_port +" \ n "); System.out. Print ("Source mac:" + ethernetpacket.getsourceaddress () + "Purpose Mac:" + Ethernetpacket.getdestin
				Ationaddress () + "\ n");
				System.out.print ("agreement:" + tcppacket.protocol + "\ n");
				System.out.print ("Data: \ n");
				try {System.out.println (new String (Tcppacket.data, "utf-8")); } catch (Unsupportedencodingexception e) {//TODO auto-generated catch block E.printstacktrace ();	
			}//for (int i = 0; i < tcpPacket.data.length; i++)//System.out.print ((char) tcppacket.data[i));
		}} public static String GetIP () {inetaddress inet = null;
	    	try {inet = Inetaddress.getlocalhost ();  String IP = inet.gethostaddress ();
		The IP that is taken on some Linux machines is the host-bound native IP, that is, the 127.0.0.1 return IP;
	}catch (Exception e) {} return null; }}<span style= "White-space:normal; Background-color: #ffffff; " > </span>

I start with an application to a query database, and then start the following program,

The output results are as follows:



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.