When I send a normal HTTP request via a socket, the server does not respond with an OK response. I copied the HTTP header from Firefox. Here is the code:
Socket s = new Socket(InetAddress.getByName("stackoverflow.com"), 80);
PrintWriter pw = new PrintWriter(s.getOutputStream());
pw.print("GET / HTTP/1.1");
pw.print("Host: stackoverflow.com");
pw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String t;
while((t = br.readLine()) != null) System.out.println(t);
br.close();
However, here is the response I received:
HTTP/1.0 408 Request Time-out
Cache-Control: no-cache
Connection: close
Content-Type: text/html
<html><body><h1>408 Request Time-out</h1>
Your browser didn't send a complete request in time.
</body></html>
I know that I can do this by using URL.openStream()
, but why doesn't the server identify the HTTP request when I send it manually?
HTTP connection is a protocol that runs on a socket.
The Raw HTTP action sends a HTTP request to a web server. How the response is treated depends on the method, but in general the status code and the response headers are returned in variables defined as part of the page load options.
Two things:
println
instead of print
to print your entries to separate lines.pw.println("");
You don't follow the HTTP RFC.
0x0d
plus 0x0a
).Generally, you should always try to use existing HTTP libraries. Although HTTP seems to be a simple protocol (and it is compared to others), it has rather strict syntactic and semantic rules. If you try to implement this yourself, you should have read and understand the relevant parts of RFC 2616 (and related).
Sadly, there are already too many crappy HTTP implementations not following the standards out there making the life for everyone miserable. Save yourself the hassle and use the HTTP libraries of your chosen language.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With