Create a simple HTTP server with Java?

后端 未结 13 2471
时光取名叫无心
时光取名叫无心 2020-11-28 04:16

What\'s the easiest way to create a simple HTTP server with Java? Are there any libraries in commons to facilitate this? I only need to respond to GET/POST, and

相关标签:
13条回答
  • 2020-11-28 04:45

    I'm suprised this example is'nt here:

    http://hc.apache.org/httpcomponents-core-ga/httpcore/examples/org/apache/http/examples/ElementalHttpServer.java

    EDIT >> The above link is not reachable. Here is an excerpt from the POST example followed by the link to the HTTP examples.

     if (!conn.isOpen()) {
            Socket socket = new Socket(host.getHostName(), host.getPort());
            conn.bind(socket);
        }
        BasicHttpEntityEnclosingRequest request = new
        BasicHttpEntityEnclosingRequest("POST",
        "/servlets‐examples/servlet/RequestInfoExample");
        request.setEntity(requestBodies[i]);
        System.out.println(">> Request URI: " + request.getRequestLine().getUri());
        httpexecutor.preProcess(request, httpproc, coreContext);
        HttpResponse response = httpexecutor.execute(request, conn, coreContext);
        httpexecutor.postProcess(response, httpproc, coreContext);
        System.out.println("<< Response: " + response.getStatusLine());
        System.out.println(EntityUtils.toString(response.getEntity()));
        System.out.println("==============");
        if (!connStrategy.keepAlive(response, coreContext)) {
        conn.close();
        } else {
        System.out.println("Connection kept alive...");
        }
    

    http://hc.apache.org/httpcomponents-core-ga/httpcore/examples/org/apache/http/examples/

    0 讨论(0)
提交回复
热议问题