1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| package http;
import io.netty.buffer.ByteBuf; 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.FullHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpVersion; import io.netty.util.CharsetUtil; import java.net.InetAddress;
public class HttpServerHandler extends ChannelInboundHandlerAdapter { private String result="";
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if(! (msg instanceof FullHttpRequest)){ result="未知请求!"; send(ctx,result,HttpResponseStatus.BAD_REQUEST); return; } FullHttpRequest httpRequest = (FullHttpRequest)msg; try{ String path=httpRequest.uri(); String body = getBody(httpRequest); HttpMethod method=httpRequest.method(); if(!"/test".equalsIgnoreCase(path)){ result="非法请求!"; send(ctx,result,HttpResponseStatus.BAD_REQUEST); return; } System.out.println("接收到:"+method+" 请求"); if(HttpMethod.GET.equals(method)){ System.out.println("body:"+body); result="GET请求"; send(ctx,result,HttpResponseStatus.OK); return; } if(HttpMethod.POST.equals(method)){ System.out.println("body:"+body); result="POST请求"; send(ctx,result,HttpResponseStatus.OK); return; }
if(HttpMethod.PUT.equals(method)){ System.out.println("body:"+body); result="PUT请求"; send(ctx,result,HttpResponseStatus.OK); return; } if(HttpMethod.DELETE.equals(method)){ System.out.println("body:"+body); result="DELETE请求"; send(ctx,result,HttpResponseStatus.OK); return; } }catch(Exception e){ System.out.println("处理请求失败!"); e.printStackTrace(); }finally{ httpRequest.release(); } }
private String getBody(FullHttpRequest request){ ByteBuf buf = request.content(); return buf.toString(CharsetUtil.UTF_8); }
private void send(ChannelHandlerContext ctx, String context,HttpResponseStatus status) { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, Unpooled.copiedBuffer(context, CharsetUtil.UTF_8)); response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8"); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println("连接的客户端地址:" + ctx.channel().remoteAddress()); ctx.writeAndFlush("客户端"+ InetAddress.getLocalHost().getHostName() + "成功与服务端建立连接! "); super.channelActive(ctx); } }
|