SOS SOS SOS PLEASE!!! I have created a primitive HttpServer in java which listens on port 80 and Uses Get method to open a file etc (127.0.0.1/index.html). ow i want create request headers (Accept,Accept Language,User-Agent) and response headers (Content-Length and Cache-Control) from HTTP/1.1 (RFC 2616) protocol. Can you help me how to do that...You will save my life!!!!!!!! Thanks!
Asked
Active
Viewed 2,658 times
1
-
Is this a homework assignment? – eykanal May 06 '11 at 15:33
-
I don't understand - if you've written your own server, it should be as simple as writing the appropriate strings to the output stream followed by new lines - that's all that headers are, `name : value` pairs following certain restrictions on where and how they appear in the response. – no.good.at.coding May 06 '11 at 15:35
-
Yea, if you actually read the RFC, you'll see how easy this is. – Will Hartung May 06 '11 at 15:41
-
1only old timers read the documentation, dude! – Vladimir Dyuzhev May 06 '11 at 15:47
1 Answers
1
Headers are just lines following initial GET/POST/* operation. Last header is separated from the content by an empty line. So all you need to do (both on the client and server sides) is to write a few lines into the request/response before the content.
HTTP/1.0 200 OK
Date: Fri, 31 Dec 1999 23:59:59 GMT
Content-Type: text/html
Content-Length: 1354
<html>
<body>
...
(more file contents)
P.S. Java has a built-in HTTP server, did you know that?
com.sun.net.HttpServer:
HttpServer httpServer = HttpServer.create(new InetSocketAddress(port), 5);
httpServer.createContext("/", new MyRequestHandler());
httpServer.setExecutor(Executors.newCachedThreadPool());
httpServer.start();

Vladimir Dyuzhev
- 18,130
- 10
- 48
- 62