【问题标题】:jnetpcap get html web page source codejnetpcap 获取html网页源代码
【发布时间】:2013-07-10 05:19:23
【问题描述】:

我正在使用 jnetpcap 来分析数据包。我要获取html网页源代码。

但是,当我使用html.page() 获取源代码时,我得到了一些看起来像二进制代码的杂乱代码。谁能帮我?如何解决?

【问题讨论】:

    标签: jnetpcap


    【解决方案1】:

    不是 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();  
    }
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      • 2012-02-27
      • 1970-01-01
      相关资源
      最近更新 更多