Я начал изучать Netty 4 Http Server, но у меня есть проблема. Как мы можем получить контент из запроса POST самым простым способом?xКак можно получить контент от POST?
Я просматриваю документацию Netty, но это сложно.
Заранее благодарен!
EDIT: Я использую этот код для получения данных.
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.handler.codec.http.multipart.HttpPostRequestDecoder $ NotEnoughDataDecoderException на io.netty. handler.codec.http.multipart.HttpPostRequestDecoder.getBodyHttpDatas (HttpPostRequestDecoder.java:339) на httpServer.HttpServerHandler.channelRead (HttpServerHandler.java:64) на io.netty.channel.AbstractChannelHandlerContext.invokeChan nelRead (AbstractChannelHandlerContext.java:333) на io.netty.channel.AbstractChannelHandlerContext.fireChannelRead (AbstractChannelHandlerContext.java:319) на io.netty.handler.codec.ByteToMessageDecoder.channelRead (ByteToMessageDecoder.java:163) на io.netty.channel.CombinedChannelDuplexHandler.channelRead (CombinedChannelDuplexHandler.java:147) на io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead (AbstractChannelHandlerContext.java:333) на io.netty.channel.AbstractChannelHandlerContext.fireChannelRead (AbstractChannelHandlerContext .java: 319) по адресу io.netty.channel.DefaultChannelPipeline.fireChann elRead (DefaultChannelPipeline.java:787) на io.netty.channel.nio.AbstractNioByteChannel $ NioByteUnsafe.read (AbstractNioByteChannel.java:130) в io.netty.channel.nio.NioEventLoop.processSelectedKey (NioEventLoop.java: 511) на io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized (NioEventLoop.java:468) на io.netty.channel.nio.NioEventLoop.processSelectedKeys (NioEventLoop.java:382) в io.netty. channel.nio.NioEventLoop.run (NioEventLoop.java:354) в io.netty.util.concurrent.SingleThreadEventExecutor $ 2.Запустите (SingleThreadEventExecutor.java:116) на io.netty.util.concurrent.DefaultThreadFactory $ DefaultRunnab leDecorator.run (DefaultThreadFactory.java:137) в java.lang.Thread.run (Thread.java:745)
Передающий код выглядит следующим образом:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "http://127.0.0.1:8030", true);
xmlhttp.send("json=value");
Предоставьте код, который вы протестировали. –