1 //web浏览器 2 //浏览器本质的原理:浏览器向服务器发请求,服务器把请求的内容返回给浏览器,然后浏览器把返回的内容绘制成一个图形化的界面 3 //Socket一种通讯交流的技术 4 //qq用户把信息通过socket传给qqServer,然后qqServer通过Socket把信息返回给qq用户 5 //创建一个Socket通讯,指定通讯格式是stream,通讯协议是TCP 6 7 //首先需要自己搭建一个cassini服务器,用自己写的浏览器去请求 8 Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp); 9 //指定该socket 通讯链接的服务器 10 socket.Connect(new DnsEndPoint("127.0.0.1", 8090)); //需要本地的cassini服务器 11 //new一个socket通讯流 12 using (Stream stream = new NetworkStream(socket)) 13 //向socket通讯中写入请求 14 using (StreamWriter sw = new StreamWriter(stream)) 15 { 16 sw.WriteLine("GET /btml5.html HTTP/1.1"); 17 sw.WriteLine("Host: 127.0.0.1:8090"); 18 sw.WriteLine();//空行回车,表示指令结束 19 } 20 //从socket通讯中读取内容 21 using (Stream stream = new NetworkStream(socket)) 22 using (StreamReader sr = new StreamReader(stream)) 23 { 24 string line; 25 while ((line = sr.ReadLine()) != null) 26 { 27 line = sr.ReadLine(); 28 Console.WriteLine(line); 29 } 30 } 31 socket.Disconnect(false); 32 Console.ReadKey();
1 using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp)) //new一个通讯 2 { 3 socket.Connect(new DnsEndPoint("www.rupeng.com", 80)); //链接到服务器 4 using (Stream stream = new NetworkStream(socket)) //通过通讯发送的请求 5 using (StreamWriter sw = new StreamWriter(stream)) 6 { 7 sw.WriteLine("GET /index.shtml HTTP/1.1"); 8 sw.WriteLine("Host:www.rupeng.com:80"); 9 sw.WriteLine(); 10 } 11 using (Stream stream = new NetworkStream(socket)) //打印出socket中所请求返回的通讯内容 12 using (StreamReader sr = new StreamReader(stream)) 13 { 14 string line; 15 while ((line = sr.ReadLine()) != null) 16 { 17 Console.WriteLine(line); 18 } 19 } 20 socket.Disconnect(false); 21 }
1 ////web服务器 2 //new一个模拟的SocketServer 3 Socket socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 4 //socketServer绑定到的Ip综节点是任何IP地址,端口是8080 5 socketServer.Bind(new IPEndPoint(IPAddress.Any, 8080)); 6 socketServer.Listen(10); //监听的最大长度是10 7 while (true) 8 { 9 Console.WriteLine("等着请求"); 10 Socket socket = socketServer.Accept(); //socketServer接收到的通讯信息后,开始新的通讯socket 11 Console.WriteLine("来了请求"); 12 using (Stream stream = new NetworkStream(socket)) //打印该通讯中的“请求” 13 using (StreamReader sr = new StreamReader(stream)) 14 { 15 string line; 16 while ((line = sr.ReadLine()) != null) 17 { 18 Console.WriteLine(line); 19 if (line.Length <= 0) 20 { 21 break; //退出本次接收到的请求,等待下一次请求 22 } 23 } 24 } 25 26 using (Stream stream = new NetworkStream(socket)) //返回该通讯请求的内容 27 using (StreamWriter sw = new StreamWriter(stream)) 28 { 29 sw.WriteLine("HTTP/1.1 200 OK"); 30 sw.WriteLine(); //htp协议规定报文头和正文之间有空行 31 sw.WriteLine("如鹏,您好,hello world"); 32 } 33 socket.Disconnect(false); //返回结束 断开该通讯 等待下一次的通讯 34 }
1 //web服务器 2 Socket socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //new一个socket通讯 3 socketServer.Bind(new IPEndPoint(IPAddress.Any, 8080)); //该通讯监听8080端口 4 socketServer.Listen(10); //监听到的请求的最大长度是10 5 while (true) 6 { 7 string firstLine = ""; 8 Console.WriteLine("等着请求"); 9 Socket socket = socketServer.Accept(); //socketServer接收到的通讯信息后,开始新的通讯s 10 Console.WriteLine("来了请求"); 11 using (Stream stream = new NetworkStream(socket)) //打印该通讯中的“请求” 12 using (StreamReader sr = new StreamReader(stream)) 13 { 14 firstLine = sr.ReadLine(); //GET /1.htm HTTP/1.1 //从第一行获得文件名url 15 string line; 16 while ((line = sr.ReadLine()) != null) 17 { 18 Console.WriteLine(line); 19 if (line.Length <= 0) 20 { 21 break; //退出本次接收到的请求,等待下一次请求 22 } 23 } 24 } 25 26 string url = ""; 27 Match match = Regex.Match(firstLine, "GET\s/(.+)\sHTTP/1\.1"); 28 if (match.Success) 29 { 30 url = match.Groups[1].Value; 31 } 32 Console.WriteLine(url); 33 34 using (Stream stream = new NetworkStream(socket)) //返回该通讯请求的内容 35 using (StreamWriter sw = new StreamWriter(stream)) 36 { 37 //根据文件url返回页面 38 string file = Path.Combine(@"F:VisualStudio_exampleExamOneselfConsole_CoreConsole_CoreFiles", url); 39 if (File.Exists(file)) 40 { 41 string html = File.ReadAllText(file); 42 sw.WriteLine("HTTP/1.1 200 OK"); 43 sw.WriteLine(); //htp协议规定报文头和正文之间有空行 44 sw.WriteLine(html); 45 } 46 else 47 { 48 sw.WriteLine("HTTP/1.1 404 NOT FOUND"); 49 sw.WriteLine(); 50 sw.WriteLine("NOT FOUND"); 51 } 52 } 53 socket.Disconnect(false); //返回结束 断开该通讯 等待下一次的通讯 54 }
1 Socket socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //new一个socket通讯 2 socketServer.Bind(new IPEndPoint(IPAddress.Any, 8080)); //该通讯监听8080端口 3 socketServer.Listen(10); //监听到的请求的最大长度是10 4 while (true) 5 { 6 string firstLine = ""; 7 Console.WriteLine("等着请求"); 8 Socket socket = socketServer.Accept(); //socketServer接收到的通讯信息后,开始新的通讯s 9 Console.WriteLine("来了请求"); 10 using (Stream stream = new NetworkStream(socket)) //打印该通讯中的“请求” 11 using (StreamReader sr = new StreamReader(stream)) 12 { 13 firstLine = sr.ReadLine(); //GET /login.html?username=admin&password=123 HTTP/1.1 //从第一行获得文件名url 14 string line; 15 while ((line = sr.ReadLine()) != null) 16 { 17 Console.WriteLine(line); 18 if (line.Length <= 0) 19 { 20 break; //退出本次接收到的请求,等待下一次请求 21 } 22 } 23 } 24 25 //对请求表头的第一行进行处理 26 string url = ""; 27 Match match = Regex.Match(firstLine, "GET\s/(.+)\sHTTP/1\.1"); 28 if (match.Success) 29 { 30 url = match.Groups[1].Value; 31 } 32 Console.WriteLine(url); 33 Dictionary<string, string> dict = ParseQueryString(url); //login.html?username=admin&password=123 34 string username = dict["username"]; 35 string password = dict["password"]; 36 37 using (Stream stream = new NetworkStream(socket)) //返回该通讯请求的内容 38 using (StreamWriter sw = new StreamWriter(stream)) 39 { 40 41 if (username == "admin" && password == "123") 42 { 43 sw.WriteLine("HTTP/1.1 200 OK"); 44 sw.WriteLine(); //htp协议规定报文头和正文之间有空行 45 sw.WriteLine("LOGIN SUCCESS"); 46 } 47 else 48 { 49 sw.WriteLine("HTTP/1.1 404 NOT FOUND"); 50 sw.WriteLine(); 51 sw.WriteLine("LOGIN ERROR"); 52 } 53 } 54 socket.Disconnect(false); //返回结束 断开该通讯 等待下一次的通讯 55 }
1 /// <summary> 2 /// 把url中“?”后面的查询字符串转字典 3 /// </summary> 4 /// <param name="url">url://login.html?username=admin&password=123</param> 5 /// <returns>包含参数的字典</returns> 6 private static Dictionary<string,string> ParseQueryString(string url) //login.html?username=admin&password=123 7 { 8 Dictionary<string, string> dict = new Dictionary<string, string>(); 9 string ss = url.Split('?')[1]; //username=admin&password=123 10 string[] strs = ss.Split('&'); 11 foreach(string str in strs) 12 { 13 string[] dds = str.Split('='); //username=admin 14 dict[dds[0]] = dds[1]; 15 } 16 return dict; 17 }