I am doing some work for the university, in which I am asked to create a TCP socket that makes an HTTP request with OPTIONS
as a header and return the whole page in a variable.
The code I have made is the following:
public String SendReq(String url, int port) throws Exception {
String resposta = null;
// Instantiate a new socket
Socket s = new Socket(url, port);
// Instantiates a new PrintWriter passing in the sockets output stream
PrintWriter wtr = new PrintWriter(s.getOutputStream());
// Prints the request string to the output stream
wtr.println("OPTIONS / HTTP/1.1");
wtr.println("Host: " + url);
wtr.println("");
wtr.flush();
// Creates a BufferedReader that contains the server response
BufferedReader bufRead = new BufferedReader(new InputStreamReader(s.getInputStream()));
String outStr;
while ((outStr = bufRead.readLine()) != null) {
resposta = resposta + outStr;
if (!outStr.trim().isEmpty()) {
resposta += "\r\n";
}
}
LSimLogger.log(Level.INFO, "response http : " + resposta);
s.close();
bufRead.close();
wtr.close();
return resposta ;
}
And actually works.
As you can see, I run this code with example.org
as url and 80
as port. In the logs, I see:
10-04-2021 21:35:49:514 tcp_client [INFO] : inici client http
10-04-2021 21:35:49:517 tcp_client [INFO] : inici HTTPclient.get
10-04-2021 21:35:49:517 tcp_client [INFO] : HTTP server_address: example.org
10-04-2021 21:35:49:517 tcp_client [INFO] : HTTP server_port: 80
10-04-2021 21:35:49:517 tcp_client [INFO] : example.org
10-04-2021 21:38:00:636 tcp_client [INFO] : response http : nullHTTP/1.1 200 OK
Allow: OPTIONS, GET, HEAD, POST
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Sat, 10 Apr 2021 19:35:49 GMT
Expires: Sat, 17 Apr 2021 19:35:49 GMT
Server: EOS (vny/0452)
Content-Length: 0
As you can see, it takes more than 3 minutes to get the response, but the date of the response is the same as the request.
What can I do to make the response faster?