14

我现在使用的代码:

    Pooled<ByteBuffer> pooledByteBuffer = exchange.getConnection().getBufferPool().allocate();
    ByteBuffer byteBuffer = pooledByteBuffer.getResource();

    int limit = byteBuffer.limit();

    byteBuffer.clear();

    exchange.getRequestChannel().read(byteBuffer);
    int pos = byteBuffer.position();
    byteBuffer.rewind();
    byte[] bytes = new byte[pos];
    byteBuffer.get(bytes);

    String requestBody = new String(bytes, Charset.forName("UTF-8") );

    byteBuffer.clear();
    pooledByteBuffer.free();

它似乎工作正常,但我不确定clear()在将其返回到池之前是否需要 ByteBuffer。我什至不确定使用exchange.getConnection().getBufferPool().allocate();. 文档中没有太多关于它的内容。

4

4 回答 4

7

读取请求正文的一种更简单的方法是分派到一个工作线程,该线程HttpExchange#getInputStream()可用。

有两种方法可以做到这一点:使用或文档BlockingHandler中显示的调度模式。下面是一个使用的例子:BlockingHandler

new BlockingHandler(myHandler)

BlockingHandler基本上为你做调度。

于 2015-01-10T14:40:15.180 回答
4

要以非阻塞方式执行此操作,请参阅io.undertow.io.Receiver界面。处理程序的示例可以是:

pathHandler.put("/test", new HttpHandler() {
    @Override
    public void handleRequest(HttpServerExchange exchange) throws Exception {
        exchange.getRequestReceiver().receiveFullBytes(new FullBytesCallback() {
            @Override
            public void handle(HttpServerExchange exchange, byte[] message) {
                System.out.println(new String(message));
            }                    
        });

        exchange.getResponseSender().send("Hello World");
    }
});

或者对于 Java 8:

pathHandler.put("/test", (ex) -> {
    ex.getRequestReceiver().receiveFullBytes((e, m) -> {
        System.out.println(new String(m));
    });
    ex.getResponseSender().send("Hello World");
});
于 2020-01-07T15:53:43.697 回答
2

@atok,我使用您的方法有一段时间,但有时当流在读取调用之前关闭时,我会得到一个空的主体。这就像魅力:

BufferedReader reader = null;
StringBuilder builder = new StringBuilder( );

try {
    exchange.startBlocking( );
    reader = new BufferedReader( new InputStreamReader( exchange.getInputStream( ) ) );

    String line;
    while( ( line = reader.readLine( ) ) != null ) {
        builder.append( line );
    }
} catch( IOException e ) {
    e.printStackTrace( );
} finally {
    if( reader != null ) {
        try {
            reader.close( );
        } catch( IOException e ) {
            e.printStackTrace( );
        }
    }
}

String body = builder.toString( );
于 2015-02-04T16:16:45.290 回答
0
import io.undertow.Handlers;
import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.BlockingHandler;


public static void main(String[] args) {

    Undertow server = Undertow.builder()
            .addHttpListener(8087, "xx.xx.xx.xx")
            .setHandler(Handlers.pathTemplate().add("/webhook", new BlockingHandler(new ItemHandler())))
            .build();
    server.start();
}

static class ItemHandler implements HttpHandler {


    @Override
    public void handleRequest(HttpServerExchange exchange) throws Exception {
       exchange.getInputStream());
    }
}
于 2019-04-24T19:20:06.470 回答