关于http协议的理论知识,我在这里就不详细说明了,具体下面给出的链接有。接下来都是用具体的操作显示的,各位可以结合起来看。
一、使用nc打开端口,并使用浏览器进行访问 (对应文章中的HTTP协议详解之请求篇)
nc -lp 8888 #使用nc打开本地的8888端口
使用浏览器,在地址栏上输入http://localhost:8888 进行访问(提出请求),此时nc界面上就会有得到一个请求的HTTP协议,具体的请求信息如下:
GET / HTTP/1.1 Host: localhost:8888 Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36 Accept-Encoding: gzip,deflate,sdch Accept-Language: zh-CN,zh;q=0.8,en;q=0.6
请求后nc没有给出回应的话,浏览器会一直在该页面进行等待。如果手动结束nc程序的话,由于没有给出回应信息,浏览器会给出无法访问该页面。
二、nc后面接着一个资源文件(对应文章中的HTTP协议详解之响应篇)
首先写一个html的helloworld
1 <html> 2 <head> 3 <title>welcome</title> 4 <head> 5 <body> 6 <h1>Hello World</h1> 7 </body> 8 </html>
然后再命令行中输入
nc -lp 8888 < helloworld.html
再浏览器中进行访问就可以得到一个页面了
我们打开浏览器的开发工具,chrome浏览器的快捷键是F12,再network中可以看到下面信息
可以看到里面有200 OK这个响应类别号
三、代码实现一个简单的服务器(这里给出一个网上的java实现)
没有办法,http服务器,就要用到Socket编程,而c++在这一方面又没有具体的标准。所以会有linux和windows下的不同,不过java在这方面就没有问题了,先给出个java版的尝尝鲜。代码虽然多,但是具体不难理解。
1 import java.io.*; 2 import java.net.ServerSocket; 3 import java.net.Socket; 4 5 6 public class SingleFileHTTPServer extends Thread { 7 8 private byte[] content; 9 private byte[] header; 10 private int port=80; 11 12 private SingleFileHTTPServer(String data, String encoding, 13 String MIMEType, int port) throws UnsupportedEncodingException { 14 this(data.getBytes(encoding), encoding, MIMEType, port); 15 } 16 17 public SingleFileHTTPServer(byte[] data, String encoding, String MIMEType, int port)throws UnsupportedEncodingException { 18 this.content=data; 19 this.port=port; 20 String header="HTTP/1.0 200 OK\r\n"+ 21 "Server: OneFile 1.0\r\n"+ 22 "Content-length: "+this.content.length+"\r\n"+ 23 "Content-type: "+MIMEType+"\r\n\r\n"; 24 this.header=header.getBytes("ASCII"); 25 } 26 27 public void run() { 28 try { 29 ServerSocket server=new ServerSocket(this.port); 30 System.out.println("Accepting connections on port "+server.getLocalPort()); 31 System.out.println("Data to be sent:"); 32 System.out.write(this.content); 33 34 while (true) { 35 Socket connection=null; 36 try { 37 connection=server.accept(); 38 OutputStream out=new BufferedOutputStream(connection.getOutputStream()); 39 InputStream in=new BufferedInputStream(connection.getInputStream()); 40 41 StringBuffer request=new StringBuffer(); 42 while (true) { 43 int c=in.read(); 44 if (c=='\r'||c=='\n'||c==-1) { 45 break; 46 } 47 request.append((char)c); 48 49 } 50 51 //如果检测到是HTTP/1.0及以后的协议,按照规范,需要发送一个MIME首部 52 if (request.toString().indexOf("HTTP/")!=-1) { 53 out.write(this.header); 54 } 55 56 out.write(this.content); 57 out.flush(); 58 59 } catch (IOException e) { 60 // TODO: handle exception 61 }finally{ 62 if (connection!=null) { 63 connection.close(); 64 } 65 } 66 } 67 68 } catch (IOException e) { 69 System.err.println("Could not start server. Port Occupied"); 70 } 71 } 72 73 public static void main(String[] args) { 74 try { 75 String contentType="text/plain"; 76 if (args[0].endsWith(".html")||args[0].endsWith(".htm")) { 77 contentType="text/html"; 78 } 79 80 InputStream in=new FileInputStream(args[0]); 81 ByteArrayOutputStream out=new ByteArrayOutputStream(); 82 int b; 83 while ((b=in.read())!=-1) { 84 out.write(b); 85 } 86 byte[] data=out.toByteArray(); 87 88 //设置监听端口 89 int port; 90 try { 91 port=Integer.parseInt(args[1]); 92 if (port<1||port>65535) { 93 port=80; 94 } 95 } catch (Exception e) { 96 port=80; 97 } 98 99 String encoding="ASCII"; 100 if (args.length>2) { 101 encoding=args[2]; 102 } 103 104 Thread t=new SingleFileHTTPServer(data, encoding, contentType, port); 105 t.start(); 106 107 } catch (ArrayIndexOutOfBoundsException e) { 108 System.out.println("Usage:java SingleFileHTTPServer filename port encoding"); 109 }catch (Exception e) { 110 System.err.println(e);// TODO: handle exception 111 } 112 } 113 }