simple HTTP server in Java using only Java SE API

前端 未结 17 1903
无人共我
无人共我 2020-11-22 13:28

Is there a way to create a very basic HTTP server (supporting only GET/POST) in Java using just the Java SE API, without writing code to manually parse HTTP requests and man

17条回答
  •  无人及你
    2020-11-22 14:07

    Try this https://github.com/devashish234073/Java-Socket-Http-Server/blob/master/README.md

    This API has creates an HTTP server using sockets.

    1. It gets a request from the browser as text
    2. Parses it to retrieve URL info, method, attributes, etc.
    3. Creates dynamic response using the URL mapping defined
    4. Sends the response to the browser.

    For example the here's how the constructor in the Response.java class converts a raw response into an http response:

    public Response(String resp){
        Date date = new Date();
        String start = "HTTP/1.1 200 OK\r\n";
        String header = "Date: "+date.toString()+"\r\n";
        header+= "Content-Type: text/html\r\n";
        header+= "Content-length: "+resp.length()+"\r\n";
        header+="\r\n";
        this.resp=start+header+resp;
    }
    

提交回复
热议问题