关键字: 一个简单的socket实现的http响应服务器(get/post/upload)
- package test.io;
-
- import java.net.*;
- import java.io.*;
-
-
-
-
-
-
-
- public class MyWebServer {
- public static void main(String[] args) {
- Socket socket = null;
- try {
-
- ServerSocket s = new ServerSocket(8000, 3);
- System.out.println("MyWebServer等待来自浏览器的连接\n");
- while (true) {
- socket = s.accept();
- System.out.println("连接已建立。端口号:" + socket.getPort());
- new MyWebServerThread(socket).start();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- class MyWebServerThread extends Thread {
- private Socket socket;
-
- MyWebServerThread(Socket socket) {
- this.socket = socket;
- }
-
- @Override
- public void run() {
- try {
- InputStreamReader is = new InputStreamReader(socket.getInputStream());
- char[] bs = new char[2048];
- PrintStream out;
- out = new PrintStream(socket.getOutputStream());
- StringBuilder msg = new StringBuilder();
-
-
-
- socket.setSoTimeout(10);
-
-
-
- int len = -1;
- try {
- while ((len = is.read(bs)) != -1) {
- msg.append(bs, 0, len);
- msg.append("\n");
- }
- } catch (Exception ex) {
-
- }
-
-
- out.println("HTTP/1.1 200 OK");
- out.println("Content-Type:text/html;charset:GBK");
- out.println();
-
- out
- .println(""
- + ""
- + "
HTTP协议测试服务器,当前时间:"
- + new java.util.Date()
- + ""
- + "
" - + "
" - + ""
- + "您提交的数据如下:
"
+ msg.toString() + "
"); - out.flush();
- out.close();
- is.close();
- System.out.println("close");
-
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- package test.io;
-
- import java.net.*;
- import java.io.*;
-
-
-
-
-
-
- public class MyWebServer {
- public static void main(String[] args) {
- Socket socket = null;
- try {
-
- ServerSocket s = new ServerSocket(8000, 3);
- System.out.println("MyWebServer等待来自浏览器的连接\n");
- while (true) {
- socket = s.accept();
- System.out.println("连接已建立。端口号:" + socket.getPort());
- new MyWebServerThread(socket).start();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- class MyWebServerThread extends Thread {
- private Socket socket;
-
- MyWebServerThread(Socket socket) {
- this.socket = socket;
- }
-
- @Override
- public void run() {
- try {
- InputStreamReader is = new InputStreamReader(socket.getInputStream());
- char[] bs = new char[2048];
- PrintStream out;
- out = new PrintStream(socket.getOutputStream());
- StringBuilder msg = new StringBuilder();
-
-
-
- socket.setSoTimeout(10);
-
-
-
- int len = -1;
- try {
- while ((len = is.read(bs)) != -1) {
- msg.append(bs, 0, len);
- msg.append("\n");
- }
- } catch (Exception ex) {
-
- }
-
-
- out.println("HTTP/1.1 200 OK");
- out.println("Content-Type:text/html;charset:GBK");
- out.println();
-
- out
- .println(""
- + ""
- + "
HTTP协议测试服务器,当前时间:"
- + new java.util.Date()
- + ""
- + "
" - + "
" - + ""
- + "您提交的数据如下:
"
+ msg.toString() + ""); - out.flush();
- out.close();
- is.close();
- System.out.println("close");
-
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- package test.io;
-
- import java.net.*;
- import java.io.*;
-
-
-
-
-
-
- public class MyWebServer {
- public static void main(String[] args) {
- Socket socket = null;
- try {
-
- ServerSocket s = new ServerSocket(8000, 3);
- System.out.println("MyWebServer等待来自浏览器的连接\n");
- while (true) {
- socket = s.accept();
- System.out.println("连接已建立。端口号:" + socket.getPort());
- new MyWebServerThread(socket).start();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- class MyWebServerThread extends Thread {
- private Socket socket;
-
- MyWebServerThread(Socket socket) {
- this.socket = socket;
- }
-
- @Override
- public void run() {
- try {
- InputStreamReader is = new InputStreamReader(socket.getInputStream());
- char[] bs = new char[2048];
- PrintStream out;
- out = new PrintStream(socket.getOutputStream());
- StringBuilder msg = new StringBuilder();
-
-
-
- socket.setSoTimeout(10);
-
-
-
- int len = -1;
- try {
- while ((len = is.read(bs)) != -1) {
- msg.append(bs, 0, len);
- msg.append("\n");
- }
- } catch (Exception ex) {
-
- }
-
-
- out.println("HTTP/1.1 200 OK");
- out.println("Content-Type:text/html;charset:GBK");
- out.println();
-
- out
- .println(""
- + ""
- + "
HTTP协议测试服务器,当前时间:"
- + new java.util.Date()
- + ""
- + "
" - + "
" - + ""
- + "您提交的数据如下:
"
+ msg.toString() + ""); - out.flush();
- out.close();
- is.close();
- System.out.println("close");
-
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
运行结果
GET方法
引用:
GET /?username=111&password=222 HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer:
POST方法
引用:
POST /?username=111&password=222 HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer:
Content-Type: application/x-www-form-urlencoded
Content-Length: 25
username=111&password=222
UPLOAD
引用:
POST /?username=111&password=222 HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer:
Content-Type: multipart/form-data; boundary=---------------------------23757983230932
Content-Length: 1704
-----------------------------23757983230932
Content-Disposition: form-data; name="phototitle"
12
-----------------------------23757983230932
Content-Disposition: form-data; name="photo"; filename="技术考核题.txt"
Content-Type: text/plain
技术考核
本次是个编程题,题目选Java/语言进行回答。
问题描述:
假设你的爱好是集邮。目前总共有N种不同的邮票,编号为从0到N-1.每种邮票的价钱被定义在数组int[] prices中(序号从0开始,数组的第i个元素表示第i种邮票的价格)。
你的目标是收集尽可能多种的邮票。你当前拥有的邮票存储在int[] have这个数组中。初始时,你没有钱,但是你可以卖掉已有邮票来买不同的邮票。返回你能收集到的不同种邮票的最大数量。
定义:
class: PostmarksCollection
method: numberOfDistinctPostmarks
Parameters: int[], int[]
Returns: int
Method signature: public int numberOfDistinctPostmarks(int[] prices, int[] have)
//为了进行测试,必须保证是public
约束:
1) N从1到50
2)prices中的元素数量刚好是N个元素
3)prices中的元素的值,从1到1,000,000
4)have中的元素数量是0个到N个
5)have中的每个元素是不同的
6)have中的每个元素的值是从0到N-1.
例如:
1)
{13,10,14,20}
{3,0,2,1}
Returns: 4
你已经拥有所有种类的邮票。
2)
{7,5,9,7}
{}
Returns: 0
你开始没有任何邮票,所以你也不能组任何事情。
3)
{4,13,9,1,5}
{1,3,2}
Returns: 4
卖掉邮票2,买入邮票0和4,(邮票2的价钱是9,邮票0的价钱是4,邮票4的价钱是5,卖掉2刚好买入0和4)
4)
{16,32,13,2,17,10,8,8,20,17}
{7,0,4,1,6,8}
Returns: 8
--------------------------------------------------------------------------------
使用Java请在此处答题(请使用JDK5.0编译,编译不能通过者不计算分数)
-----------------------------23757983230932--
WebServer的实现就是HTTP通信的过程。其中包括服务器和浏览器的以下几个步骤。
(1)建立TCP连接。建立一个TCP连接,对80端口(WebServer默认端口)进行监听,接收到TCP包后即进入TCP回调函数。
(2)Web浏览器向Web服务器发送请求命令。一旦建立了连接,浏览器即向服务器发送请求命令。请求消息包括请求行,首部,之后浏览器发送了一空白行来通知服务器,它已经结束了该头信息的发送。例如:
GET/index.html HTTP/1.1
(3)Web服务器响应。客户机向服务器发出请求后,服务器会客户机回送响应。响应消息包括状态行、响应头、空行及实体内容。在响应报文中,包括协议版本号和应答状态码,关于它自己的数据及被请求的文档信息。例如:
HTTP/1.1 200 OK
Content-type:text/html
(4)关闭连接。数据传送完毕,双方通过4次握手,结束TCP/IP连接
阅读(16322) | 评论(0) | 转发(0) |