IHttpRequestHandler filter = new IHttpRequestHandler() {
public void onRequest(final IHttpExchange exchange) throws IOException {
PostRequest newRequest = new PostRequest(exchange.getRequest().getRequestUrl().toString(), "text/plain", "Hello");
IHttpResponseHandler respHdl = new IHttpResponseHandler() {
public void onResponse(IHttpResponse response) throws IOException {
exchange.send(response);
}
public void onException(IOException ioe) {
}
};
exchange.forward(newRequest, respHdl);
}
};
chain.addLast(filter);
chain.addLast(new EchoHandler());
IServer server = new HttpServer(0, chain);
ConnectionUtils.start(server);
PostRequest req = new PostRequest("http://localhost:" + server.getLocalPort() + "/", "text/plain", "test123456");
req.setHeader("Host", "localhost");
req.setHeader("User-Agent", "me");
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.write(req.toString());
String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
int contentLength = QAUtil.readContentLength(header);
String body = con.readStringByLength(contentLength);