a 只是想打印所有包,如下所示。它是简单的 HTTP 分析器 java 代码。请看我的主要内容。我如何打印协议(protocol)名称或其他功能。谢谢您
//package jdumper.analyzer;
import jpcap.packet.*;
import java.util.*;
import java.io.*;
public class HTTPAnalyzer extends JDPacketAnalyzer
{
private static HTTPAnalyzer analy;
public static void main(String arg[] )
{
System.out.println(boom.getValueNames());;
}
private static final String[] valueNames={
"Method",
"Header"
};
String method;
Vector headers=new Vector();
public HTTPAnalyzer(){
layer=APPLICATION_LAYER;
}
public boolean isAnalyzable(Packet p){
if(p instanceof TCPPacket &&
(((TCPPacket)p).src_port==80 || ((TCPPacket)p).dst_port==80))
return true;
else return false;
}
public String getProtocolName(){
return "HTTP";
}
public String[] getValueNames(){
return valueNames;
}
public void analyze(Packet p){
method="";
headers.removeAllElements();
if(!isAnalyzable(p)) return;
try{
BufferedReader in=new BufferedReader(new StringReader(new String(p.data)));
method=in.readLine();
if(method==null || method.indexOf("HTTP")==-1){
// this packet doesn't contain HTTP header
method="Not HTTP Header";
return;
}
String l;
//read headers
while((l=in.readLine()).length()>0)
headers.addElement(l);
}catch(IOException e){}
}
public Object getValue(String valueName){
if(valueNames[0].equals(valueName)) return method;
if(valueNames[1].equals(valueName)) return headers;
return null;
}
Object getValueAt(int index){
if(index==0) return method;
if(index==1) return headers;
return null;
}
public Object[] getValues(){
Object[] values=new Object[2];
values[0]=method;
values[1]=headers;
return values;
}
}
最佳答案
问题就在这里。 HTTP 是基于流的协议(protocol) - 它在 TCP/IP 流上运行。为了准确地解析/提取 HTTP 流的 header ,您首先需要从数据包中重新组装流。一旦你(准确地)完成了这一点,你就可以解析流并提取 header 。但是,如果您只是查看 HTTP 流中的各个数据包,则无法可靠地判断哪些数据包包含 header ,哪些数据包是 HTTP 请求或响应正文的一部分。
<小时/>忽略这个问题,我很难弄清楚 jpcap/jpcap/jdumper/无论你的类应该针对什么版本构建。这使得很难弄清楚应该如何使用 JDPacketAnalyzer 的子类。但我很确定它不应该有 main
方法。
关于java - 如何打印所有包HTTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32347963/