-2

Basically I have created a java server which reads incoming packets, but the problem is when a post packet comes it doesn't read the body portion that contains the actual data, it shows the message as POST /index.html http/1.1.

Here is how i read the data on my server:

try{
    IR = new InputStreamReader(socket.getInputStream());
}catch(Exception ie){
    System.out.println("Cound'nt create IR");
}

BufferedReader BR = new BufferedReader(IR);
String MESSAGE = null;
try{
    MESSAGE = BR.readLine();
}catch(Exception ie){
    System.out.println("Cound'nt Receive Message");
}
System.out.println(MESSAGE);

Could you please tell me how to read the body of the received packet?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
rocky
  • 35
  • 6

2 Answers2

1

if you use Java 8 you can do

List<String> content = BR.lines().collect(Collectors.toList());

Otherwise you can do

List<String> content = new ArrayList<>();
String line;
while((line = BR.readLine()) != null) {
   content.add(line);
}

Then parse/format as you wish to get the response as a whole.

Laksitha Ranasingha
  • 4,321
  • 1
  • 28
  • 33
  • okay your answer worked but now it never gets out of the loop. List content = new ArrayList<>(); String line; try { while((line = BR.readLine()) != null) { content.add(line); } } catch (IOException e1) { e1.printStackTrace(); } for (int i = 0; i < content.size(); i++) { System.out.println(content.get(i)); }" as you can see i have create a for loop to print out the arraylist, but when i request through my browser it stucks in loading and the console never gets to for loop until i close my browser tab that i request from. – rocky Aug 10 '16 at 14:26
0

BR.readLine() only returns the first line; you need to loop through the BufferedReader to read all lines. One possible way would be:

String fullMessage;
String aux;
do{
    aux = BR.readLine();
    fullMessage = fullMessage.concat(aux);
}while(aux != null);

This is just an example to illustrate the idea.

Read the documentation: https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine()

Example for java 7+

    String message = null;
    //Try with resources, java will handle the closing of the stream, event if exception is thrown.
    try (   InputStreamReader inputStream = new InputStreamReader(socket.getInputStream());
            BufferedReader bufferReader = new BufferedReader(inputStream);) {

        String aux = null;
        do {
            aux = bufferReader.readLine();
            message = message.concat(aux);
        } while (aux != null);

    } catch (IOException e) {
        System.out.println("Failed to read input stream from socket");
    }

    System.out.println("Message: " + message);

For older versions of java you can use:

    InputStreamReader inputStream = null;
    BufferedReader bufferReader = null;
    String message = null;
    try {
        inputStream = new InputStreamReader(socket.getInputStream());
        bufferReader = new BufferedReader(inputStream);
        String aux = null;
        do {
            aux = bufferReader.readLine();
            message = message.concat(aux);
        } while (aux != null);

        inputStream.close();
        bufferReader.close();
    } catch (IOException e) {
        //Read throws IOException, don't just use Exception (this could hide other exceptions that you are not treating).
        System.out.println("Failed to read input stream from socket");
    } finally{
        //Use 2 try-catch, if you only use one and the first fails, the second will never close.
        try{inputStream.close();}catch(IOException ioe){}
        try{bufferReader.close();}catch(IOException ioe){}
    } 


     System.out.println("Message: "+message);

EDIT: Sorry append is for StringBuffer and StringBuilder, my bad sorry.

EDIT2: Added 2 example with more information.