首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jnetpcap获取html网页源代码

jnetpcap获取html网页源代码
EN

Stack Overflow用户
提问于 2013-07-10 13:19:24
回答 1查看 544关注 0票数 1

我正在使用jnetpcap来分析数据包。我想得到html网页的源代码。

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

EN

回答 1

Stack Overflow用户

发布于 2015-03-05 21:34:24

我不是jnetpcap专家,但我使用这个类已经有一段时间了,它似乎很有效。它实际上获得了许多HTTP字段,包括它的有效负载。

代码语言:javascript
复制
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();  
}
}

希望能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17562918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档