2014-09-14 4 views
0

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"); 
+0

일부 코드는 테스트를 거쳐 제공됩니다. –

답변

0

은 당신이 같은데 여기에 약간의 것들을 혼합. 디코더는 아마도 ServerBootstrap에서 호출 된 파이프 라인 초기화 프로그램에 더 잘 앉을 수 있습니다.

아래의 예는 매닝의 책 Netty in Action에서 추출됩니다

public class HttpDecoderEncoderInitializer 
     extends ChannelInitializer<Channel> { 
     private final boolean client; 
     public HttpDecoderEncoderInitializer(boolean client) { 
      this.client = client; 
     } 
     @Override 
     protected void initChannel(Channel ch) throws Exception { 
      ChannelPipeline pipeline = ch.pipeline(); 
      if (client) { 
       pipeline.addLast("decoder", new HttpResponseDecoder()); 
       pipeline.addLast("encoder", new HttpRequestEncoder()); 
      } else { 
       pipeline.addLast("decoder", new HttpRequestDecoder()); 
       pipeline.addLast("encoder", new HttpResponseEncoder()); 
      } 
     } 
    } 

이 API의 문서/소스 코드는 HTTP 예제를 따라 몇 가지 쉬운있다 (예를 들어 폴더에).

파이프 라인에 디코더를 넣으면 처리기의 req 개체에서 직접 작동 할 수 있습니다. 이는 HttpMessage 개체로 바이트에서 "디코딩"됩니다.

관련 문제