サーバーを作成できるソケットの数をテストしたいので、ソケットを使用して http 要求を作成します。そのため、 と を使用してサーバーから書き込みと読み取りをOutputStream
行いInputStream
ます。しかし、最初の応答の後、入力ストリームから再度読み取ることはできません。ソケットを閉じずに 2 番目の応答を読み取る方法を知っていますか?
これが私のコードです:
Socket socket = new Socket();
socket.connect(new InetSocketAddress(address, 80), 1000);
socket.setSoTimeout(25*1000);
OutputStream os = socket.getOutputStream();
os.write(getRequest(host)); // some request as bytearray, it has Connection: Keep-Alive in the header
os.flush();
InputStream is = socket.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
String response = IOUtils.toString(bis);
System.out.println("RESPONSE = \n" + response); // this works fine
os.write(getRequestBodyBa()); // send another request, i can see it sent to server with wireshark
os.flush();
// try to read again but it always return empty string
response = IOUtils.toString(bis); // how to read the second response?????
System.out.println("RESPONSE = \n" + response);
os.close();
is.close();
socket.close();
ありがとう。