package org.llgc; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.apache.http.ExceptionLogger; import org.apache.http.HttpException; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.config.SocketConfig; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.bootstrap.HttpServer; import org.apache.http.impl.bootstrap.ServerBootstrap; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpProcessor; import org.apache.http.protocol.HttpProcessorBuilder; import org.apache.http.protocol.HttpRequestHandler; import org.apache.http.protocol.RequestConnControl; import org.apache.http.protocol.RequestContent; import org.apache.http.protocol.RequestExpectContinue; import org.apache.http.protocol.RequestTargetHost; import org.apache.http.protocol.RequestUserAgent; public class Main { private static class MyHttpRequestHandler implements HttpRequestHandler { @Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { response.setStatusCode(HttpStatus.SC_OK); response.setEntity(new StringEntity("some important message", ContentType.TEXT_PLAIN)); } } public static void main(String[] args) throws IOException, InterruptedException { HttpRequestHandler requestHandler = new MyHttpRequestHandler(); HttpProcessor httpProcessor = HttpProcessorBuilder.create() // Required protocol interceptors .add(new RequestContent()).add(new RequestTargetHost()) // Recommended protocol interceptors .add(new RequestConnControl()).add(new RequestUserAgent("MyAgent-HTTP/1.1")) // Optional protocol interceptors .add(new RequestExpectContinue(true)).build(); SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(15000).setTcpNoDelay(true).build(); final HttpServer server = ServerBootstrap.bootstrap().setListenerPort(8080).setHttpProcessor(httpProcessor) .setSocketConfig(socketConfig).setExceptionLogger(new StdErrorExceptionLogger()) .registerHandler("*", requestHandler).create(); server.start(); server.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { server.shutdown(5, TimeUnit.SECONDS); } }); } static class StdErrorExceptionLogger implements ExceptionLogger { @Override public void log(final Exception ex) { ex.printStackTrace(); } } }