java nio http服务器(1)读取http请求
来源:互联网 发布:数据质量评估方法 编辑:程序博客网 时间:2024/05/22 01:34
通过nio读取http的头信息。直接看代码:
package com.hcserver.conn;import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.StringReader;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.SelectionKey;import java.nio.channels.Selector;import java.nio.channels.ServerSocketChannel;import java.nio.channels.SocketChannel;import java.util.Iterator;public class Server {public void start() throws Exception {Selector selector = Selector.open();ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();serverSocketChannel.configureBlocking(false);serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);serverSocketChannel.socket().setReuseAddress(true);serverSocketChannel.socket().bind(new InetSocketAddress(8084));while(true){while (selector.select() > 0) {Iterator<SelectionKey> selectedKeys = selector.selectedKeys() .iterator();while (selectedKeys.hasNext()) {SelectionKey key = selectedKeys.next();if (key.isAcceptable()) {ServerSocketChannel ssc = (ServerSocketChannel) key.channel();SocketChannel channel = ssc.accept();if(channel != null){channel.configureBlocking(false);channel.register(selector, SelectionKey.OP_READ);// 客户socket通道注册读操作}} else if (key.isReadable()) {SocketChannel channel = (SocketChannel) key.channel();channel.configureBlocking(false);String receive = receive(channel);BufferedReader b = new BufferedReader(new StringReader(receive));String s = b.readLine();while (s !=null) {System.out.println(s);s = b.readLine();}b.close();channel.register(selector, SelectionKey.OP_WRITE);} else if (key.isWritable()) {SocketChannel channel = (SocketChannel) key.channel();String hello = "hello world...";ByteBuffer buffer = ByteBuffer.allocate(1024);byte[] bytes = hello.getBytes();buffer.put(bytes);buffer.flip();channel.write(buffer);channel.shutdownInput();channel.close();}}}}}// 接受数据private String receive(SocketChannel socketChannel) throws Exception {ByteBuffer buffer = ByteBuffer.allocate(1024);byte[] bytes = null;int size = 0;ByteArrayOutputStream baos = new ByteArrayOutputStream();while ((size = socketChannel.read(buffer)) > 0) {buffer.flip();bytes = new byte[size];buffer.get(bytes);baos.write(bytes);buffer.clear();}bytes = baos.toByteArray();return new String(bytes);}}
package com.hcserver;import com.hcserver.conn.Server;public class ServerStartUp {/** * @param args * @throws Exception */public static void main(String[] args) throws Exception {Server server = new Server();server.start();}}
运行代码,然后打开浏览器在地址栏输入:http://localhost:8084/hello?aa=1&bb=2,
回车后浏览器显示hello world 。而后台则输出http请求头信息:如下所示:
GET /hello?aa=1&bb=2 HTTP/1.1Accept: text/html, application/xhtml+xml, */*Accept-Language: zh-CNUser-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)Accept-Encoding: gzip, deflateHost: localhost:8084DNT: 1Connection: Keep-Alive
0 0
- java nio http服务器(1)读取http请求
- java nio http服务器(4)小结
- java使用NIO构造http请求
- HTTP 服务器开发(Java)--HTTP请求
- java nio http服务器(3)简单的Servlet容器
- 一个基于java NIO的Http服务器
- NIO 简单http 服务器
- nio 模拟http服务器
- java读取http请求中的body
- java读取http请求中的body
- java读取http请求中的body
- NIO--SocketChannel发送HTTP请求
- NIO--SocketChannel发送HTTP请求
- 读取HTTP请求头
- 读取HTTP请求头
- 读取HTTP请求头
- [Java通信]Java通过HTTP请求发送HTTP服务器
- JAVA发送HTTP请求(post、get),读取HTTP响应内容,实例及应用
- leetcode 11 Linked List Cycle
- Linux命令行编辑的快捷键
- Cocos2d-x 3.1.1 Lua示例 ActionEaseTest(动作)
- Maximum Depth of Binary Tree
- 基于朴素贝叶斯的内容推荐算法
- java nio http服务器(1)读取http请求
- 61、实例 jQuery管理页面中的多个库
- Java面试题
- 计算出某年某月某日是星期几
- touch
- 对Oracle 语句缓存设置和JDBC PreparedStatement的试验
- SqlServer数据库还原或更新 脱机和联机操作
- Android 侧边栏快速索引(点击索引、滑动索引),通讯录样式
- 获取手机唯一标示符