Results 1 to 6 of 6
  1. #1
    eaepsao is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Question How do I recognize, when HTTP response ends?

    I have 'raw' headers, which I'm sending to a web servers (code below). But HTTP 1.1 is persistent and some servers (despite HTTP/1.0 and "Connection: close" header) simply won't close connection and I'm not able to end the while loop. Some servers even doesn't send Content-Length header. I know about HttpUrlConnection, HttpClient etc., but these doesn't support sending 'raw' headers to servers (AFAIK).

    So, my question is: does exist a guaranteed way how to close a connection, when all data are sent (in HTTP/1.1)?

    Java Code:
    Socket connectionToServer = new Socket(host, port);
    OutputStream outToServer = connectionToServer.getOutputStream();
    
    outToServer.write(myRawHeaders.getBytes());
    
    InputStream inputFromServer = connectionToServer.getInputStream();
    byte[] buff = new byte[1024];
    ByteOutputStream dataBuffer = new ByteOutputStream();
    
    int count;
    while ((count = inputFromServer.read(buff)) != -1) {
        dataBuffer.write(buff, 0, count);
    }
                
    System.out.write(dataBuffer.getBytes(), 0, dataBuffer.getCount());
    Thanks for any help.

  2. #2
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: How do I recognize, when HTTP response ends?

    In simple way you can restrict time for connection

    Calendar is good way to get time in java

    I will look otherways

  3. #3
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: How do I recognize, when HTTP response ends?

    why don't you use thread for listening from connected socket. Just use 1 thread to listen until you close the connection. On the other hand you can send data.

  4. #4
    eaepsao is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: How do I recognize, when HTTP response ends?

    Quote Originally Posted by Grkn View Post
    why don't you use thread for listening from connected socket. Just use 1 thread to listen until you close the connection. On the other hand you can send data.
    Thanks for reply!
    So, first thread is reading and sending data to another thread, which is writing the data to output?

  5. #5
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: How do I recognize, when HTTP response ends?

    I am not good at English so I can not express myself. :)

    I guess the problem is not about persistent connection.
    while ((count = inputFromServer.read(buff)) != -1) { dataBuffer.write(buff, 0, count);}

    you do not have to use while loop, inputFromServer.read(buff) method uses loop. This method derived from .read() method;
    your byte array length is 1024 so .read(buff) method tries to read 1024 byte from stream by using .read() method;
    if stream tells the read() method that it is end of transmission then read(buff) method gives the length of information; // you keep that information in count variable.

    Maybe another problem occurs in here. If Http does not give "end of transmission" EOT then read() method waits for information and blocks thread to continue its work.
    You can not do anything except setting time of connection;// socket.setTimeout(1000); -> 1 second after connection it terminates the connection on server

    There is another way ofcourse but you have to code it. You can give up using read(buff) method.
    Use read() method and you set flag variable for each bit receive and you can check that flag is or is not changed in 2 seconds or 1 minute then that means stream does not give data to me and you can close socket.
    //socket.close();
    Example:
    boolean flag=false;
    int receivedData=-1;
    checkFlag();//method-> use this method in a new thread; Purpose of this method : checks the flag variable that is changed or not in given time period if not changed close Socket
    while(true){
    flag=true;
    receivedData=in.read();
    flag=false;
    }

  6. #6
    eaepsao is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: How do I recognize, when HTTP response ends?

    Thank You for great solution.

Similar Threads

  1. Reg: HTTP Response Headers
    By royzz in forum Networking
    Replies: 3
    Last Post: 03-30-2011, 11:13 PM
  2. Manipulate HTTP response
    By nikos in forum Networking
    Replies: 1
    Last Post: 10-07-2010, 05:56 PM
  3. write http response into file?
    By DaBernie in forum Networking
    Replies: 3
    Last Post: 10-10-2009, 11:21 PM
  4. HTTP response not read
    By SD_Be in forum Networking
    Replies: 1
    Last Post: 03-10-2009, 04:36 PM
  5. Does any file in an FTP server ends up in an HTTP server?
    By islamfunny in forum CLDC and MIDP
    Replies: 4
    Last Post: 08-15-2008, 04:30 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •