我已经向Jnetpcap论坛提出了这个问题。没有得到任何回应。我一直在尝试从请求/响应数据包中获取网站名称。这就是我尝试过的:
PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {
final Tcp tcp = new Tcp();
final Http http = new Http();
final Ip4 ip = new Ip4();
public void nextPacket(PcapPacket packet, String user) {
if (packet.hasHeader(http)) {
packet.getHeader(http);
final String content_length = http.fieldValue(Response.Content_Length);
final String response_code = http.fieldValue(Response.ResponseCode);
//Find if the given packet is a Request/Response Pkt : First get the TCP header
packet.getHeader(tcp);
Integer int_tcp_source = new Integer(tcp.source());
Integer int_tcp_destination = new Integer(tcp.destination());
if(int_tcp_source!=80 && content_length==null){
//It is a Request pkt :
packet.getHeader(http);
final String ref = http.fieldValue(Request.Referer);
final String req_url = http.fieldValue(Request.RequestUrl);
String page_url = http.fieldValue(Request.Host);
System.out.printf("n Referer " +ref +req_url );//Get the URL
System.out.printf("nHost " +page_url);
}
}
}
};
但它甚至没有进入:
if (packet.hasHeader(http)) {
我正在使用windows笔记本电脑和无线连接
如果我尝试:
if (packet.hasHeader(ip)) {//Ip4
我可以进入if块,并可以检索源和目标IP地址。
我基本上需要做与http://jnetpcap.com/?q=node/937.
我真的很困,真的需要别人的帮助。
或者有人能帮我弄清楚如何从数据包中获取网站名称吗?一些代码片段会很棒
有人能帮忙吗。
提前谢谢。
以下代码有效:
PcapPacketHandler<String> handler = new PcapPacketHandler<String>() {
// Protocol handlers
private final Tcp tcp = new Tcp();
private final Http http = new Http();
@Override
public void nextPacket(PcapPacket packet, String userString) {
if (!packet.hasHeader(tcp)) {
return; // not a TCP package, skip
}
if (!packet.hasHeader(http)) {
return; // not a HTTP package, skip
}
if (http.isResponse()) {
return; // not a HTTP request, skip
}
System.out.println("Referer: " + http.fieldValue(Request.Referer));
System.out.println("Request URL: " + http.fieldValue(Request.RequestUrl));
System.out.println("Host: " + http.fieldValue(Request.Host));
}
}
如果我运行这个http://www.stackoverflow.com,我得到以下输出:
Referer: https://www.google.com/
Request URL: /
Host: stackoverflow.com
Referer: http://stackoverflow.com/
Request URL: /Js/stub.en.js?v=eb1d547a9670
Host: cdn.sstatic.net
Referer: http://stackoverflow.com/
Request URL: /Sites/stackoverflow/all.css?v=4e41902aa7a6
Host: cdn.sstatic.net
...
你确定你没有试图访问HTTPS资源吗?在这种情况下,对packet.hasHeader(http)
的调用将失败。