Answer the question
In order to leave comments, you need to log in
How to make keep-alive requests to the server?
I'm trying to make 2 https requests
SSLSocket socket = (SSLSocket) ssl.createSocket("site.net",443);
socket.setReuseAddress(true);
socket.setKeepAlive(true);
socket.setTcpNoDelay(true);
socket.startHandshake();
socket.getOutputStream().write(testRequest());
InputStream input = socket.getInputStream();
int res = 0;
byte[] buffer = new byte[1024*64];
while (((res = input.read(buffer, 0, buffer.length)) != -1)&&!Thread.interrupted()) {
System.out.println(new String(buffer,0,res, Charset.defaultCharset()));
}
// вернул всё как положено, в заголовках ответа сервера keep-alive поддерживается
socket.getOutputStream().write(testRequest()); // Ещё один запрос
while (((res = input.read(buffer, 0, buffer.length)) != -1)&&!Thread.interrupted()) {
System.out.println(new String(buffer,0,res, Charset.defaultCharset()));
}
// Но тут уже ничего не приходит ((
Answer the question
In order to leave comments, you need to log in
Hang up friends)) I slept, debugged and saw that the first cycle was blocked by the read function because the connection is not closed and -1 is not returned.
Solution:
boolean reading = true;
while (reading&&!Thread.interrupted()) {
reading = ((res = input.read(buffer, 0, buffer.length)) != -1);
if (buffer[res]==0x0){
reading = false;
}
System.out.println(new String(buffer,0,res, Charset.defaultCharset()));
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question