S
S
syxme2021-01-30 00:52:47
Java
syxme, 2021-01-30 00:52:47

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()));
       }
      // Но тут уже ничего не приходит ((


What can be wrong ?
Maybe after I have received all the data, I need to somehow tell the server that I have finished reading?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
syxme, 2021-01-30
@syxme

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 question

Ask a Question

731 491 924 answers to any question