开发者

Http-Server How to Create Request-Headers and response-Headers

开发者 https://www.devze.com 2023-03-03 23:51 出处:网络
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 he

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!


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号