2013-07-10 192 views
1

我正在使用jnetpcap來分析數據包。我想獲得html網頁源代碼。jnetpcap獲取html網頁源代碼

但是,當我使用html.page()獲取源代碼時,我得到了一些看起來像二進制代碼的雜亂代碼。誰能幫我?如何解決它?

回答

0

不是jnetpcap的專家,但我一直在使用這個類一段時間,它似乎工作。它實際上獲得了許多HTTP字段,包括其有效載荷。

package br.com.mvalle.ids.sniffer; 

import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 

import org.jnetpcap.Pcap; 
import org.jnetpcap.PcapIf; 
import org.jnetpcap.packet.PcapPacket; 
import org.jnetpcap.packet.PcapPacketHandler; 
import org.jnetpcap.packet.format.FormatUtils; 
import org.jnetpcap.protocol.network.Ip4; 
import org.jnetpcap.protocol.tcpip.Http; 
import org.jnetpcap.protocol.tcpip.Tcp; 

public class Sniffer { 
private List<PcapIf> alldevs = new ArrayList<PcapIf>(); // Will be filled with NICs 
private StringBuilder errbuf = new StringBuilder(); // For any error msgs 
private PcapIf selectedDevice; 
private Pcap pcap; 
private PcapPacketHandler<String> jpacketHandler; 

public Sniffer(){ 
    listDevices(); 
    selectedDevice = selectDevice(1); 
    openDevice(selectedDevice); 
    packetHandler(); 
    capturePackets(); 
} 

public void listDevices(){ 
    int r = Pcap.findAllDevs(alldevs, errbuf); 
    if (r == Pcap.NOT_OK || alldevs.isEmpty()) { 
      System.err.printf("Can't read list of devices, error is %s", errbuf.toString()); 
     return; 
    } 

    System.out.println("Network devices found:"); 

    int i = 0; 
    for (PcapIf device : alldevs) { 
     String description = 
      (device.getDescription() != null) ? device.getDescription() 
       : "No description available"; 
     System.out.printf("#%d: %s [%s]\n", i++, device.getName(), description); 
    } 
} 


private PcapIf selectDevice(int deviceId){ 
    PcapIf device = alldevs.get(1); // We know we have atleast 1 device (parameter changed from 0 to 1) 
    System.out 
     .printf("\nChoosing '%s' on your behalf:\n", 
      (device.getDescription() != null) ? device.getDescription() 
       : device.getName()); 
    return device; 
} 


private void openDevice (PcapIf device){ 
    int snaplen = 64 * 1024;   // Capture all packets, no trucation 
    int flags = Pcap.MODE_PROMISCUOUS; // capture all packets 
    int timeout = 10 * 1000;   // 10 seconds in millis 
    pcap = 
     Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf); 

    if (pcap == null) { 
     System.err.printf("Error while opening device for capture: " 
      + errbuf.toString()); 
     return; 
    }   
} 


private void packetHandler(){ 
    jpacketHandler = new PcapPacketHandler<String>() { 
    Http httpheader = new Http(); 

    public void nextPacket(PcapPacket packet, String user) { 
     if(packet.hasHeader(httpheader)){ 
      System.out.println(httpheader.toString()); 
      if(httpheader.hasPayload()){ 
       System.out.println("HTTP payload: (string length is " 
        +new String(httpheader.getPayload()).length()+")"); 
       System.out.println(new String(httpheader.getPayload())); 
       System.out.println("HTTP truncated? " 
        +httpheader.isPayloadTruncated()); 
      } 
      //System.out.println(packet.toString()); 
     }} 
    }; 
} 


private void capturePackets(){ 
    pcap.loop(pcap.LOOP_INFINITE , jpacketHandler, "Received Packet"); 
    pcap.close(); 
} 
} 

希望它有幫助。