Reputation: 2597
I am new to java just learned it this week, my teacher want me parse a HTTP request saved in text file format. Basically he wanted me to extract
just this line "GET /fenway/ HTTP/1.0\r\n" and this "Host: www.redsox.com\r\n"
and save it in a text format.
My program doesn't work well yet as it contains error. What I did was to read a text file line by line and then enter it in to a function that tries to read the buffer and tokenizes each word and saves it to copyGetWord, then running a while loop to detect if the word "GET" is found it will store getWord to an arraylist.
Another thing is that am only allowed to use java for this no perl or python. Also I have tried jnetpcap libraries but have problem installing them so am stuck with text files.
Hope anyone can help.
Transmission Control Protocol, Src Port: bridgecontrol (1073), Dst Port: http (80), Seq: 1, Ack: 1, Len: 270 Hypertext Transfer Protocol GET /azur/ HTTP/1.0\r\n Connection: Keep-Alive\r\n User-Agent: Mozilla/4.08 [en] (WinNT; I)\r\n Host: www.google.com\r\n
public static void main(String[] args) {
System.out.println("Tries to read the samplepcap1.txt, \n");
ReadingFile1 handle = new ReadingFile1();
ArrayList <String> StoreLine = new ArrayList<String>();
try{
FileReader ReadPcap = new FileReader("samplePcapSmall.txt");
BufferedReader bufferPcap = new BufferedReader(ReadPcap);
String readBufLine = bufferPcap.readLine();
StoreLine.add(readBufLine);
while (readBufLine!=null){
readBufLine = bufferPcap.readLine();
handle.LookForWord(readBufLine);
}
bufferPcap.close();
}
catch (FileNotFoundException e){
System.out.println("\nFile not found");
}
catch (IOException e){
System.out.println("Problem reading the file.\n");
}
}
public String LookForWord(String getWord){
ArrayList <String>MatchingWord = new ArrayList<String>();
StringTokenizer copyGetWord = new StringTokenizer(getWord," ");
while (copyGetWord.hasMoreElements()){
if(copyGetWord.nextToken().matches("GET")){
MatchingWord.add(getWord);
}
}
System.out.println("\nMatch: "+MatchingWord);
return getWord;
}
Tries to read the samplepcap1.txt, stores in an ArrayList and Displays ArrayList
Match: []
Match: [ GET /fenway/ HTTP/1.0\r\n]
Match: []
Match: []
Match: []
Exception in thread "main" java.lang.NullPointerException
at java.util.StringTokenizer.<init>(StringTokenizer.java:182)
at java.util.StringTokenizer.<init>(StringTokenizer.java:204)
at readingfile1.ReadingFile1.LookForWord(ReadingFile1.java:84)
at readingfile1.ReadingFile1.main(ReadingFile1.java:62)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
Upvotes: 1
Views: 386
Reputation: 11638
Your while loop is the issue. Try this instead:
String readBufLine = bufferPcap.readLine();
while (readBufLine!=null){
StoreLine.add(readBufLine);
handle.LookForWord(readBufLine);
readBufLine = bufferPcap.readLine();
}
bufferPcap.close();
This reads the line from the buffer and loops while the read line is not null - the new line is always read as the last action of the body of the loop so that the next iteration of the loop will check that the read line is not null prior to attempting to tokenize it.
Upvotes: 1