Netty_使用http协议,post&get方式

  1. jboss.netty中使用http协议的get请求方式较为简单, 但是post还是没有思路.
  2. io.netty中可以适用http协议实现post请求方式
post是属于安全的请求方式,不像get是明文的方式.
在 Java SDK 中有一个叫 JSSE(javax.net.ssl)包,这个包中提供了一些类来建立 SSL/TLS 连接。通过这些类,开发者就可以忽略复杂的协议建立流程,较为简单地在网络上建成安全的通讯通道。JSSE 包中主要包括以下一些部分:
  • 安全套接字(secure socket)和安全服务器端套接字

  • 非阻塞式 SSL/TLS 数据处理引擎(SSLEngine)

  • 套接字创建工厂 , 用来产生 SSL 套接字和服务器端套接字

  • 套接字上下文 , 用来保存用于创建和数据引擎处理过程中的信息

  • 符合 X.509 规范密码匙和安全管理接口
=========================== netty服务代码===========================
package com.chis.backup;

import static io.netty.handler.codec.http.HttpHeaderNames. CONTENT_LENGTH ;
import static io.netty.handler.codec.http.HttpHeaderNames. CONTENT_TYPE ;
import static io.netty.handler.codec.http.HttpHeaderNames. EXPIRES ;
import static io.netty.handler.codec.http.HttpResponseStatus. OK ;
import static io.netty.handler.codec.http.HttpVersion. HTTP_1_1 ;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.QueryStringDecoder;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Charsets;

/**
 * 业务处理类
 * @author ChenZhenJian
 *
 */
public class HttpServerInboundHandler extends   ChannelHandlerAdapter {

       private static final Logger LOGGER = LoggerFactory.getLogger(HttpServerInboundHandler. class );

//    private static final Pattern SEND_TASK_FOR_METHOD_GET_PATTERN = Pattern.compile("/dmap-sf/query(?:\\?.*)?");

//    private static final Pattern SEND_TASK_FOR_METHOD_POST_PATTERN = Pattern.compile("/dmap-sf/sendMsg(?:\\?.*)?");

       private HttpRequest request ;
       private boolean isGet ;
       private boolean isPost ;

       /**
       * POST: http://localhost:8844/dmap - sf/sendMsg?hello=df&world=women body: we
       * are togather
       *
       * GET: http://localhost:8844/dmap - sf/query?hello=df&world=women
       */
       @Override
       public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
             //1 msg是HttpRequest
             if (msg instanceof HttpRequest) {
                   request = (HttpRequest) msg;
                  String uri = request .uri();
                  HttpMethod method = request .method();
                   isGet = method.equals(HttpMethod. GET );
                   isPost = method.equals(HttpMethod. POST );
                  System. out .println(String. format( "Uri:%s method %s" , uri, method));
                   //如果是get
//                if (SEND_TASK_FOR_METHOD_GET_PATTERN.matcher(uri).matches() && isGet) {
//                      System.out.println("doing something here.");
//                      String param = "hello";
//                      String str = getParamerByNameFromGET(param);
//                      System.out.println( param + ":" + str);
//                }
                   if ( isGet ) {
                        System. out .println( "doing something here." );
                        String param = "hello" ;
                        String str = getParamerByNameFromGET(param);
                        System. out .println(param + ":" + str);
                  }
//                if (SEND_TASK_FOR_METHOD_POST_PATTERN.matcher(uri).matches() && isPost) {
//                      System.out.println("doing something here.");
//                }
                   if ( isPost ) {
                        System. out .println( "doing something here." );
                  }
                   else {
                        String responseString = "返回的数据-------" ;
                        writeHttpResponse(responseString, ctx, OK );
                  }

            }

             //2 msg是HttpContent
             if (! isGet ) {
                  System. out .println( "isPost" );
                   if (msg instanceof HttpContent) {
                        HttpContent content = (HttpContent) msg;
                        ByteBuf buf = content.content();
                        String bodyString = buf.toString(Charsets.UTF_8);
                        System. out .println( "body: " + bodyString);
                        String l = getParamerByNameFromPOST( "key" , bodyString);
                        System. out .println( "参数:" +l);
                        buf.release();
                        writeHttpResponse( "post返回成功!" , ctx, OK );
                  }
            }
      }

       @Override
       public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            ctx.flush();
      }

       @Override
       public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
             LOGGER .error(cause.getMessage());
            ctx.close();
      }

       private String getParamerByNameFromGET(String name) {
            QueryStringDecoder decoderQuery = new QueryStringDecoder( request .uri());
             return getParameterByName(name, decoderQuery);
      }

       private String getParamerByNameFromPOST(String name, String body) {
            QueryStringDecoder decoderQuery = new QueryStringDecoder( "some?" + body);
             return getParameterByName(name, decoderQuery);
      }

       /**
       * 根据传入参数的key获取value
       * @param name
       * @param decoderQuery
       * @return
       */
       private String getParameterByName(String name, QueryStringDecoder decoderQuery) {
            Map> uriAttributes = decoderQuery.parameters();
             for (Entry> attr : uriAttributes.entrySet()) {
                  String key = attr.getKey();
                   for (String attrVal : attr.getValue()) {
                         if (key.equals(name)) {
                               return attrVal;
                        }
                  }
            }
             return null ;
      }
       private void writeHttpResponse(String responseString, ChannelHandlerContext ctx, HttpResponseStatus status)
                   throws UnsupportedEncodingException {
            FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1 , OK );
             // 设置缓存大小
//          ByteBuffer byteBuffer = new ByteBuffer();
//          byteBuffer.size();
//          byteBuffer.append("恭喜你,成功了!");
             byte [] fileToByte = this .fileToByte( "f://test.jpg" );
            response.content().writeBytes(fileToByte);
            response.headers().set( CONTENT_TYPE , "image/png; charset=UTF-8" );
            response.headers().setInt( CONTENT_LENGTH , response.content().writerIndex());
//          Channel ch = ctx.channel();
//          ch.write(response);
            ctx.write(response);
            ctx.flush();
            ctx.close();
      }
       @SuppressWarnings ( "unused" )
       private void writeHttpResponse1(String responseString, ChannelHandlerContext ctx, HttpResponseStatus status)
                   throws UnsupportedEncodingException {
//          FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.wrappedBuffer(responseString
//                      .getBytes(Charsets.UTF_8)));
//          response.headers().set(CONTENT_TYPE, "text/ json");
//          response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
//          response.headers().set(EXPIRES, 0);
//          if (HttpHeaders.isKeepAlive(request)) {
//                response.headers().set(CONNECTION, Values.KEEP_ALIVE);
//          }
            System. out .println( "开始写入返回数据..." );
            FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1 , status, Unpooled.wrappedBuffer(responseString
                        .getBytes( Charsets.UTF_8)));
//          response.content().writeBytes(responseString.getBytes());
            response.headers().set( CONTENT_TYPE , "text/html" );
            response.headers().setInt( CONTENT_LENGTH , response.content().readableBytes());
            response.headers().setInt( EXPIRES , 0);
//          if (HttpHeaders.isKeepAlive(request)) {
//                response.headers().set(CONNECTION, Values.KEEP_ALIVE);
//          }
            Channel ch = ctx.channel();
            ch.write(response);
//          ch.disconnect();
            ch.close();
//          ctx.write(response);
//          ctx.flush();
//          ctx.close();
      }
      
       /**
       * 本地图片的文件流
       *
       * @param filename   本地文件地址
       * @return
       */
       private byte [] fileToByte(String filename) {
             byte [] b = null ;
            BufferedInputStream is = null ;
             try {
                  System. out .println( "开始>>>>" + System.currentTimeMillis());
                  File file = new File(filename);
                  b = new byte [( int ) file.length()];
                  is = new BufferedInputStream( new FileInputStream(file));
                  is.read(b);
            } catch (FileNotFoundException e) {
                  e.printStackTrace();
            } catch (IOException e) {
                  e.printStackTrace();
            } finally {
                   if (is != null ) {
                         try {
                              is.close();
                        } catch (IOException e) {
                              e.printStackTrace();
                        }
                  }
            }
             return b;
      }

}


===================启动类====================
package com.chis.backup;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
/**
 * netty_http启动类
 * @author ChenZhenJian
 *
 */
public class HttpServer { 
 
     
    public void start( int port) throws Exception { 
        EventLoopGroup bossGroup = new NioEventLoopGroup(); 
        EventLoopGroup workerGroup = new NioEventLoopGroup(); 
        try
            ServerBootstrap b = new ServerBootstrap(); 
            b.group(bossGroup, workerGroup).channel(NioServerSocketChannel. class
                    .childHandler( new ChannelInitializer() { 
                                @Override  
                                public void initChannel(SocketChannel ch) throws Exception { 
                                    ch.pipeline().addLast( new HttpResponseEncoder()); 
                                    ch.pipeline().addLast( new HttpRequestDecoder()); 
                                    ch.pipeline().addLast( new HttpServerInboundHandler()); 
                                } 
                            }).option(ChannelOption. SO_BACKLOG , 128)  
                    .childOption(ChannelOption. SO_KEEPALIVE , true ); 
 
            ChannelFuture f = b.bind(port).sync(); 
 
            f.channel().closeFuture().sync(); 
        } finally
            workerGroup.shutdownGracefully(); 
            bossGroup.shutdownGracefully(); 
        } 
    } 
 
    public static void main(String[] args) throws Exception { 
        HttpServer server = new HttpServer(); 
        System. out .println( "Http Server listening on 10006 ..." );
        server.start(10006); 
    } 
}

你可能感兴趣的:(Netty_使用http协议,post&get方式)