setrequestproperty:基于netty构建一个http服务

 2021-07-19 9:02    77  

Java专栏推荐

客户端代码package com.huanfeng.test;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.URL;import java.net.URLConnection;import java.util.List;import java.util.Map;public class Test11 { public static void main(String[] args) { int num=100; for (int i=0;i<num;i++){ new Thread(new Runnable() { @Override public void run() { String s=Test11.sendGet(":8082"); System.out.println(s); } }).start(); } } public static String sendGet(String url) { String result = ""; BufferedReader in = null; try { String urlNameString = url; URL realUrl = new URL(urlNameString); // 打开和URL之间的连接 URLConnection connection = realUrl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立实际的连接 connection.connect(); // 获取所有响应头字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍历所有的响应头字段 for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } // 定义 BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送GET请求出现异常setrequestproperty!" + e); e.printStackTrace(); } // 使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; }}服务端代码package com.huanfeng.test;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioServerSocketChannel;public class Test10 { public static void main(String[] args) throws Exception{ EventLoopGroup bossGroup=new NioEventLoopGroup(1); EventLoopGroup workerGroup=new NioEventLoopGroup(); try{ ServerBootstrap serverBootstrap=new ServerBootstrap(); serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class).childHandler(new TestServerInitializer()); ChannelFuture channelFuture =serverBootstrap.bind(8082).sync(); channelFuture.channel().closeFuture().sync(); }finally{ bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }}package com.huanfeng.test;import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;import io.netty.handler.codec.*;import io.netty.util.CharsetUtil;import kotlin.random.Random;//客户端和服务段相互通信的数据封装成 class Test extends SimpleChannelInboundHandler<> { @Override protected void channelRead0(ChannelHandlerContext ctx, msg) throws Exception { if(msg instanceof ){ System.out.println("msg 类型"+msg.getClass()); System.out.println("客户端地址"+ctx.channel().remoteAddress()); //回复信息给浏览器 ByteBuf content= Unpooled.copiedBuffer("hello,我是服务器", CharsetUtil.UTF_8); //构建一个 response = new DefaultFull(, , content); response.headers().set(,"text/plain"); response.headers().set(,content.readableBytes()); //将构建好的response进行返回 ctx.writeAndFlush(response); } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); }}package com.huanfeng.test;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelPipeline;import io.netty.channel.socket.SocketChannel;import io.netty.handler.codec.;public class TestServerInitializer extends ChannelInitializer<SocketChannel> { @Override protected void initChannel(SocketChannel ch) throws Exception { //向管道加入处理器 //得到管道 ChannelPipeline pipeline = ch.pipeline(); //加入一个netty提供的 //编解码器 //("My",new ()); //添加自定义的处理器 pipeline.addLast("MyTest",new Test()); }}

本文标签:一个基于构建

原文链接:https://www.xgfox.com/jsyd/1044.html

本文版权:如无特别标注,本站文章均为原创。