Netty 4 Http Server 학습을 시작했지만 이미 문제가 있습니다. 가장 간단한 방법으로 POST 요청에서 콘텐츠를 얻을 수있는 방법은 무엇입니까?x POST에서 콘텐츠를 가져 오는 방법은 무엇입니까?
Netty의 설명서를 탐색하고 있지만 복잡합니다.
미리 감사드립니다.
편집 : 나는 데이터를 수신하기 위해이 코드를 사용하고 있습니다. io.netty에서
io.netty.handler.codec.http.multipart.HttpPostRequestDecoder $ NotEnoughDataDecoderException :
import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.HttpHeaders; import static io.netty.handler.codec.http.HttpHeaders.Names.*; import static io.netty.handler.codec.http.HttpHeaders.Values; import io.netty.handler.codec.http.HttpRequest; import static io.netty.handler.codec.http.HttpResponseStatus.*; import static io.netty.handler.codec.http.HttpVersion.*; import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder; import io.netty.handler.codec.http.multipart.InterfaceHttpData; import java.io.IOException; import java.util.List; public class HttpServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelReadComplete(ChannelHandlerContext ctx) { ctx.flush(); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws IOException { if (msg instanceof HttpRequest) { HttpRequest req = (HttpRequest) msg; if (HttpHeaders.is100ContinueExpected(req)) { ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); } boolean keepAlive = HttpHeaders.isKeepAlive(req); /* HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(req); List<InterfaceHttpData> list = decoder.getBodyHttpDatas(); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } */ String message = "Lorem ipsum dolorem"; byte[] byty = message.getBytes(); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(byty)); response.headers().set(ACCESS_CONTROL_ALLOW_ORIGIN, "*"); response.headers().set(CONTENT_TYPE, "text/plain; charset=utf-8"); response.headers().set(CONTENT_LENGTH, response.content().readableBytes()); if (!keepAlive) { ctx.write(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(CONNECTION, Values.KEEP_ALIVE); ctx.write(response); } } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); } }
당신이 볼 수 있듯이이 세 가지가 나에게 예외를 돌려 줄 댓글을 달았습니다. io.netty.channel.AbstractChannelHandlerContext.invokeChan에서 httpServer.HttpServerHandler.channelRead (HttpServerHandler.java:64) 에서 handler.codec.http.multipart.HttpPostRequestDecoder.getBodyHttpDatas (HttpPostRequestDecoder.java:339) nelRead io.netty.handler.codec.ByteToMessageDecoder.channelRead (ByteToMessageDecoder.java:163)에서 io.netty.channel.AbstractChannelHandlerContext.fireChannelRead (AbstractChannelHandlerContext.java:319)에서 (AbstractChannelHandlerContext.java:333) 에서 io.netty.channel.CombinedChannelDuplexHandler.channelRead io.netty.channel.AbstractChannelHandlerContext.fireChannelRead에서 io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead (AbstractChannelHandlerContext.java:333)에서 (CombinedChannelDuplexHandler.java:147) (AbstractChannelHandlerContext .java : 319) at io.netty.channel.DefaultChannelPipeline.fireChann io.netty.channel.nio.NioEventLoop.processSelectedKey에서 io.netty.channel.nio.AbstractNioByteChannel $ NioByteUnsafe.read (AbstractNioByteChannel.java:130) (NioEventLoop.java에서 elRead (DefaultChannelPipeline.java:787) : io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized (NioEventLoop.java:468) io.netty.channel.nio.NioEventLoop.processSelectedKeys (NioEventLoop.java:382) io.netty에서 에서 에서 511) . io.netty.util.concurrent.DefaultThreadFactory $ DefaultRunnab에서 io.netty.util.concurrent.SingleThreadEventExecutor $의 2.run (SingleThreadEventExecutor.java:116) 에서 channel.nio.NioEventLoop.run (NioEventLoop.java:354) java.lang.Thread.run에서 leDecorator.run (DefaultThreadFactory.java:137) (Thread.java:745는)
보내는 코드는 다음과 같습니다
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "http://127.0.0.1:8030", true);
xmlhttp.send("json=value");
일부 코드는 테스트를 거쳐 제공됩니다. –