Reputation: 646
I am creating an HTTP client, which handles HTTP requests and responses with a socket. It is able to send the first request and read the response stream. However the subsequent requests do not write anything to the input stream.
static String host/* = some host*/;
static int port/* = some port*/;
private Socket sock = null;
private BufferedWriter outputStream = null;
private BufferedReader inputStream = null;
public HttpClient() throws UnknownHostException, IOException {
sock = new Socket(host, port);
outputStream = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
inputStream = new BufferedReader(new InputStreamReader(sock.getInputStream()));
sock.setKeepAlive(true);
sock.setTcpNoDelay(true);
}
/* ... */
public String sendGetRequest(String relAddr) throws IOException {
outputStream.write("GET " + relAddr + " HTTP/1.0\r\n");
outputStream.write("\r\n");
outputStream.flush();
String line;
StringBuffer buff = new StringBuffer();
while((line = inputStream.readLine()) != null) {
buff.append(line);
buff.append("\n");
}
return buff.toString();
}
In the main method, I use the following:
client = new HttpClient();
str = client.sendGetRequest(addr);
System.out.println(str);
/* and again */
str = client.sendGetRequest(addr);
System.out.println(str);
But only the first sendGetRequest invoke returns a response string. The subsequent ones do not. Do you have an idea?
Upvotes: 4
Views: 3079
Reputation: 53694
HTTP 1.0 does not support persistent connections as part of the actual specification (apparently there is an unofficial extension). You need to switch to 1.1 (or use the unofficial extension, assuming the server supports it).
Upvotes: 3