当前位置: 首页 > news >正文

订阅号怎么做免费的视频网站吗北京住房城乡建设网站

订阅号怎么做免费的视频网站吗,北京住房城乡建设网站,豆瓣wordpress,做服装加工哪个网站比较好Netty是一个以事件驱动的异步通信网络框架#xff0c;可以帮助我们实现多种协议的客户端和服务端通信#xff0c;话不多说#xff0c;上代码#xff0c;需要引入下方依赖 dependencygroupIdio.netty/groupIdartifactIdnetty-all/artif…Netty是一个以事件驱动的异步通信网络框架可以帮助我们实现多种协议的客户端和服务端通信话不多说上代码需要引入下方依赖 dependencygroupIdio.netty/groupIdartifactIdnetty-all/artifactIdversion4.1.42.Final/version/dependencydependencygroupIdorg.msgpack/groupIdartifactIdmsgpack/artifactIdversion0.6.12/version/dependencydependencygroupIdorg.slf4j/groupIdartifactIdslf4j-api/artifactIdversion1.7.30/version/dependencydependencygroupIdch.qos.logback/groupIdartifactIdlogback-core/artifactIdversion1.2.4/version/dependencydependencygroupIdcom.itextpdf/groupIdartifactIditextpdf/artifactIdversion5.5.8/version/dependencydependencygroupIdorg.bouncycastle/groupIdartifactIdbcprov-jdk15on/artifactIdversion1.49/versiontypejar/typescopecompile/scopeoptionaltrue/optional/dependencydependencygroupIdorg.bouncycastle/groupIdartifactIdbcpkix-jdk15on/artifactIdversion1.49/versiontypejar/typescopecompile/scopeoptionaltrue/optional/dependency1.Server package http;import constant.Constant; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.ssl.SslContext; import io.netty.handler.ssl.SslContextBuilder; import io.netty.handler.ssl.util.SelfSignedCertificate;import java.security.cert.CertificateException;public class HttpServer {// 通过nio方式来接收连接和处理连接private static EventLoopGroup group new NioEventLoopGroup();// 服务端引导类private static ServerBootstrap b new ServerBootstrap();// 是否开启SSL模式public static final boolean SSL false;// Netty创建全部都是实现自AbstractBootstrap,客户端的是Bootstrap,服务端的则是ServerBootstrappublic static void main(String[] args) throws Exception {final SslContext sslContext;if (SSL) {SelfSignedCertificate ssc new SelfSignedCertificate();sslContext SslContextBuilder.forServer(ssc.certificate(),ssc.privateKey()).build();} else {sslContext null;}try {b.group(group).channel(NioServerSocketChannel.class)// 设置过滤器.childHandler(new ServerHandlerInit(sslContext));// 异步进行绑定ChannelFuture f b.bind(Constant.DEFAULT_PORT);// 给ChannelFuture 增加监听器f.addListener(new ChannelFutureListener() {Overridepublic void operationComplete(ChannelFuture future) throws Exception {System.out.println(绑定端口已成功....);}});System.out.println(服务端启动成功端口是: Constant.DEFAULT_PORT);System.out.println(服务器启动模式: (SSL ? SSL安全模式 : 普通模式));// 监听服务器关闭监听ChannelFuture closeFuture f.channel().closeFuture().sync();closeFuture.addListener(new ChannelFutureListener() {Overridepublic void operationComplete(ChannelFuture future) throws Exception {System.out.println(服务器已经关闭....);}});} finally {// 关闭EventLoopGroup,释放掉所有资源包括创建的线程group.shutdownGracefully();}} } package http;import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.http.HttpContentCompressor; import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpRequestDecoder; import io.netty.handler.codec.http.HttpResponseEncoder; import io.netty.handler.ssl.SslContext;public class ServerHandlerInit extends ChannelInitializerSocketChannel {private final SslContext sslContext;public ServerHandlerInit(SslContext sslContext) {this.sslContext sslContext;}Overrideprotected void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline ch.pipeline();if (sslContext ! null) {pipeline.addLast(sslContext.newHandler(ch.alloc()));}// pipeline中的handler可以自定义名称方便排查问题// 把应答报文 编码pipeline.addLast(encoder, new HttpResponseEncoder());// 把请求报文 解码pipeline.addLast(decoder, new HttpRequestDecoder());// 聚合http为一个完整的报文pipeline.addLast(aggregator,new HttpObjectAggregator(10*1024*1024));// 把应答报文压缩pipeline.addLast(compressor, new HttpContentCompressor());pipeline.addLast(new BusinessHandler());} } 2.业务处理类 package http;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.*; import io.netty.util.CharsetUtil;public class BusinessHandler extends ChannelInboundHandlerAdapter {Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {String result ;FullHttpRequest httpRequest (FullHttpRequest)msg;System.out.println(httpRequest.headers());try {// 获取路径String path httpRequest.uri();// 获取bodyString body httpRequest.content().toString(CharsetUtil.UTF_8);// 获取请求方法HttpMethod method httpRequest.method();System.out.println(接收到 method 请求);// 如果不是这个路径就直接返回错误if (!/test.equalsIgnoreCase(path)) {result 非法请求! path;send(ctx,result, HttpResponseStatus.BAD_REQUEST);return;}// 如果是GET请求if (HttpMethod.GET.equals(method)) {// 接收到的消息做业务处理...System.out.println(body : body);result GET请求,应答: RespConstant.getNews();send(ctx, result, HttpResponseStatus.OK);return ;}// 如果是其他类型请求如postif (HttpMethod.POST.equals(method)) {// 接收到的消息做业务逻辑处理// ....// return;}} catch (Exception e) {System.out.println(处理请求失败!);e.printStackTrace();} finally {// 释放请求httpRequest.release();}}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;charsetUTF-8);ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);}// 建立连接时返回消息Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {System.out.println(连接的客户端地址 : ctx.channel().remoteAddress()); // super.channelActive(ctx);} } 3.Client package http;import constant.Constant; import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelInitializer; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.http.HttpClientCodec; import io.netty.handler.codec.http.HttpContentDecompressor; import io.netty.handler.codec.http.HttpObjectAggregator;public class HttpClient {public static final String HOST 127.0.0.1;public static void main(String[] args) throws InterruptedException {if (HttpServer.SSL) {System.out.println(服务器处于SSL模式客户端不支持推出);return ;}HttpClient client new HttpClient();client.connect(Constant.DEFAULT_SERVER_IP, Constant.DEFAULT_PORT);}public void connect(String host, int port) throws InterruptedException {EventLoopGroup workerGroup new NioEventLoopGroup();try {Bootstrap b new Bootstrap();b.group(workerGroup).channel(NioSocketChannel.class).handler(new ChannelInitializerSocketChannel() {Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new HttpClientCodec());// 聚合Http为一个完整的报文ch.pipeline().addLast(aggregator,new HttpObjectAggregator(10 * 1024 * 1024));// 解压缩ch.pipeline().addLast(decompressor, new HttpContentDecompressor());ch.pipeline().addLast(new HttpClientInboundHandler());}});// start ChannelFuture f b.connect(host, port).sync();f.addListener(new ChannelFutureListener() {Overridepublic void operationComplete(ChannelFuture future) throws Exception {System.out.println(连接成功....);}});ChannelFuture closeFuture f.channel().closeFuture().sync();closeFuture.addListener(new ChannelFutureListener() {Overridepublic void operationComplete(ChannelFuture future) throws Exception {System.out.println(关闭成功...);}});} finally {workerGroup.shutdownGracefully();}}} package http;import constant.Constant; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.handler.codec.http.*; import io.netty.util.CharsetUtil;import java.net.URI;public class HttpClientInboundHandler extends ChannelInboundHandlerAdapter {Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {FullHttpResponse httpResponse (FullHttpResponse) msg;System.out.println(httpResponse.status());System.out.println(httpResponse.headers());ByteBuf buf httpResponse.content();System.out.println(buf.toString(CharsetUtil.UTF_8));httpResponse.release();}Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {System.out.println(channelActive.......);URI uri new URI(/test);String msg Hello;DefaultFullHttpRequest request new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,HttpMethod.GET,uri.toASCIIString(),Unpooled.wrappedBuffer(msg.getBytes(UTF-8)));// 构建http请求request.headers().set(HttpHeaderNames.HOST, Constant.DEFAULT_SERVER_IP);request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);request.headers().set(HttpHeaderNames.CONTENT_LENGTH, request.content().readableBytes());// 发送http请求ctx.writeAndFlush(request);// super.channelActive(ctx);}Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {cause.printStackTrace();ctx.close(); // super.exceptionCaught(ctx, cause);} } package http;import java.util.Random;public class RespConstant {private static final String[] NEWS {她那时候还太年轻不知道所有命运赠送的礼物早已在暗中标好了价格。——斯蒂芬·茨威格《断头皇后》,这是一个最好的时代也是一个最坏的时代这是一个智慧的年代这是一个愚蠢的年代\n 这是一个信任的时期这是一个怀疑的时期这是一个光明的季节这是一个黑暗的季节\n 这是希望之春这是失望之冬人们面前应有尽有人们面前一无所有\n 人们正踏上天堂之路人们正走向地狱之门。 —— 狄更斯《双城记》,};private static final Random R new Random();public static String getNews() {return NEWS[R.nextInt(NEWS.length)];} } package constant;import java.util.Date;/*** 常量*/ public class Constant {public static final Integer DEFAULT_PORT 7777;public static final String DEFAULT_SERVER_IP 127.0.0.1;// 根据输入信息拼接出一个应答信息public static String response(String msg) {return Hello, msg , Now is new Date(System.currentTimeMillis()).toString(); } } 4.总结分析 如果你想实现http请求需要把HttpServer中的SSL置为false结果如下 如果你想实现Https的请求则将SSL的变量置为true,目前的代码中是没有支持客户端的SSL请求的我们可以在postman或者chrome浏览器中查看 https://localhost:7777/test 由于我们的证书是自己设置的所以chrome浏览器认为这个证书不是有效的需要我们手动点击 IDEA中会出现红色证书错误暂时可以不用管你还可以在postman的GET请求中添加body 到此http的简易服务器就搭建好了如果你的程序出现了以下错误请检查开头的pom配置是否下载成功这是由于证书有问题才报的错
http://www.zqtcl.cn/news/616831/

相关文章:

  • 免费的个人网站怎么做企业网站管理系统软件
  • 枣庄住房和城乡建设局网站如何注册国外域名
  • 满洲里建设局网站网页设计公司的目标客户有哪些
  • 英文书 影印版 网站开发怀化组织部网站
  • 网站建设领域的基本五大策略要学会网站细节
  • dede做英文网站优化cms建站系统哪个好
  • eclipse sdk做网站邯郸技术服务类
  • 汕头网站网站建设西安网约车租车公司哪家好
  • 网站空间域名维护协议网络推广软件平台
  • 昆明网站建设公司猎狐科技怎么样wordpress主题打不开
  • 网站推广入口服饰网站建设 e-idea
  • 长沙网站建设电话2个女人做暧暧网站
  • 手机手机端网站建设电子商务网站建设步骤一般为
  • 上海金瑞建设集团网站怎样登陆网站后台
  • 定西模板型网站建设网络架构和现实架构的差异
  • 做搜索的网站做网站的代码有哪些
  • 视频制作网站推荐js做音乐网站
  • 海北wap网站建设公司有后台网站怎么做
  • 织梦网站最新漏洞入侵外贸网站模板有什么用
  • 在跨境网站贸易公司做怎么样网站建设维护合同范本
  • 网站必须做可信认证南山网站制作
  • 如何使用mysql数据库做网站企业管理专业大学排名
  • 九江网站建设九江深圳网站建设费用大概多少
  • 万网站长工具郑州seo哪家公司最强
  • 宁波哪里可以做网站企业网站源码哪个好
  • 网站每天点击量多少好精选聊城做网站的公司
  • 网站建设课程基础兰州网站seo费用
  • 天助可以搜索别人网站曲靖网站推广
  • 易语言编程可以做网站么网站备案流程
  • 我想接加工单seo搜索引擎优化工资