Results 1 to 6 of 6
- 10-15-2011, 02:41 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
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)?
Thanks for any help.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());
- 10-16-2011, 03:54 AM #2
Member
- Join Date
- Oct 2011
- Posts
- 65
- Rep Power
- 0
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
- 10-16-2011, 03:59 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 65
- Rep Power
- 0
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.
- 10-16-2011, 01:36 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
- 10-16-2011, 07:38 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 65
- Rep Power
- 0
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;
}
- 10-17-2011, 03:32 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Reg: HTTP Response Headers
By royzz in forum NetworkingReplies: 3Last Post: 03-30-2011, 11:13 PM -
Manipulate HTTP response
By nikos in forum NetworkingReplies: 1Last Post: 10-07-2010, 05:56 PM -
write http response into file?
By DaBernie in forum NetworkingReplies: 3Last Post: 10-10-2009, 11:21 PM -
HTTP response not read
By SD_Be in forum NetworkingReplies: 1Last Post: 03-10-2009, 04:36 PM -
Does any file in an FTP server ends up in an HTTP server?
By islamfunny in forum CLDC and MIDPReplies: 4Last Post: 08-15-2008, 04:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks