ASP.NET CORE Web浏览器和Web服务器

  //web浏览器
//浏览器本质的原理:浏览器向服务器发请求,服务器把请求的内容返回给浏览器,然后浏览器把返回的内容绘制成一个图形化的界面
//Socket一种通讯交流的技术
//qq用户把信息通过socket传给qqServer,然后qqServer通过Socket把信息返回给qq用户
//创建一个Socket通讯,指定通讯格式是stream,通讯协议是TCP //首先需要自己搭建一个cassini服务器,用自己写的浏览器去请求
Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
//指定该socket 通讯链接的服务器
socket.Connect(new DnsEndPoint("127.0.0.1", )); //需要本地的cassini服务器
//new一个socket通讯流
using (Stream stream = new NetworkStream(socket))
//向socket通讯中写入请求
using (StreamWriter sw = new StreamWriter(stream))
{
sw.WriteLine("GET /btml5.html HTTP/1.1");
sw.WriteLine("Host: 127.0.0.1:8090");
sw.WriteLine();//空行回车,表示指令结束
}
//从socket通讯中读取内容
using (Stream stream = new NetworkStream(socket))
using (StreamReader sr = new StreamReader(stream))
{
string line;
while ((line = sr.ReadLine()) != null)
{
line = sr.ReadLine();
Console.WriteLine(line);
}
}
socket.Disconnect(false);
Console.ReadKey();

web浏览器:向本机的Cassini服务器发请求并打印返回的内容

 using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp)) //new一个通讯
{
socket.Connect(new DnsEndPoint("www.rupeng.com", )); //链接到服务器
using (Stream stream = new NetworkStream(socket)) //通过通讯发送的请求
using (StreamWriter sw = new StreamWriter(stream))
{
sw.WriteLine("GET /index.shtml HTTP/1.1");
sw.WriteLine("Host:www.rupeng.com:80");
sw.WriteLine();
}
using (Stream stream = new NetworkStream(socket)) //打印出socket中所请求返回的通讯内容
using (StreamReader sr = new StreamReader(stream))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
socket.Disconnect(false);
}

Web浏览器:向www.rupeng.com服务器发请求并打印返回的内容

 ////web服务器
//new一个模拟的SocketServer
Socket socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//socketServer绑定到的Ip综节点是任何IP地址,端口是8080
socketServer.Bind(new IPEndPoint(IPAddress.Any, ));
socketServer.Listen(); //监听的最大长度是10
while (true)
{
Console.WriteLine("等着请求");
Socket socket = socketServer.Accept(); //socketServer接收到的通讯信息后,开始新的通讯socket
Console.WriteLine("来了请求");
using (Stream stream = new NetworkStream(socket)) //打印该通讯中的“请求”
using (StreamReader sr = new StreamReader(stream))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
if (line.Length <= )
{
break; //退出本次接收到的请求,等待下一次请求
}
}
} using (Stream stream = new NetworkStream(socket)) //返回该通讯请求的内容
using (StreamWriter sw = new StreamWriter(stream))
{
sw.WriteLine("HTTP/1.1 200 OK");
sw.WriteLine(); //htp协议规定报文头和正文之间有空行
sw.WriteLine("如鹏,您好,hello world");
}
socket.Disconnect(false); //返回结束 断开该通讯 等待下一次的通讯
}

web服务器:打印浏览器的“请求”,并返回给浏览器新的内容

             //web服务器
Socket socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //new一个socket通讯
socketServer.Bind(new IPEndPoint(IPAddress.Any, )); //该通讯监听8080端口
socketServer.Listen(); //监听到的请求的最大长度是10
while (true)
{
string firstLine = "";
Console.WriteLine("等着请求");
Socket socket = socketServer.Accept(); //socketServer接收到的通讯信息后,开始新的通讯s
Console.WriteLine("来了请求");
using (Stream stream = new NetworkStream(socket)) //打印该通讯中的“请求”
using (StreamReader sr = new StreamReader(stream))
{
firstLine = sr.ReadLine(); //GET /1.htm HTTP/1.1 //从第一行获得文件名url
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
if (line.Length <= )
{
break; //退出本次接收到的请求,等待下一次请求
}
}
} string url = "";
Match match = Regex.Match(firstLine, "GET\\s/(.+)\\sHTTP/1\\.1");
if (match.Success)
{
url = match.Groups[].Value;
}
Console.WriteLine(url); using (Stream stream = new NetworkStream(socket)) //返回该通讯请求的内容
using (StreamWriter sw = new StreamWriter(stream))
{
//根据文件url返回页面
string file = Path.Combine(@"F:\VisualStudio_example\ExamOneself\Console_Core\Console_Core\Files\", url);
if (File.Exists(file))
{
string html = File.ReadAllText(file);
sw.WriteLine("HTTP/1.1 200 OK");
sw.WriteLine(); //htp协议规定报文头和正文之间有空行
sw.WriteLine(html);
}
else
{
sw.WriteLine("HTTP/1.1 404 NOT FOUND");
sw.WriteLine();
sw.WriteLine("NOT FOUND");
}
}
socket.Disconnect(false); //返回结束 断开该通讯 等待下一次的通讯
}

web服务器:根据浏览器请求的url,返回指定的Url界面

 Socket socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //new一个socket通讯
socketServer.Bind(new IPEndPoint(IPAddress.Any, )); //该通讯监听8080端口
socketServer.Listen(); //监听到的请求的最大长度是10
while (true)
{
string firstLine = "";
Console.WriteLine("等着请求");
Socket socket = socketServer.Accept(); //socketServer接收到的通讯信息后,开始新的通讯s
Console.WriteLine("来了请求");
using (Stream stream = new NetworkStream(socket)) //打印该通讯中的“请求”
using (StreamReader sr = new StreamReader(stream))
{
firstLine = sr.ReadLine(); //GET /login.html?username=admin&password=123 HTTP/1.1 //从第一行获得文件名url
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
if (line.Length <= )
{
break; //退出本次接收到的请求,等待下一次请求
}
}
} //对请求表头的第一行进行处理
string url = "";
Match match = Regex.Match(firstLine, "GET\\s/(.+)\\sHTTP/1\\.1");
if (match.Success)
{
url = match.Groups[].Value;
}
Console.WriteLine(url);
Dictionary<string, string> dict = ParseQueryString(url); //login.html?username=admin&password=123
string username = dict["username"];
string password = dict["password"]; using (Stream stream = new NetworkStream(socket)) //返回该通讯请求的内容
using (StreamWriter sw = new StreamWriter(stream))
{ if (username == "admin" && password == "")
{
sw.WriteLine("HTTP/1.1 200 OK");
sw.WriteLine(); //htp协议规定报文头和正文之间有空行
sw.WriteLine("LOGIN SUCCESS");
}
else
{
sw.WriteLine("HTTP/1.1 404 NOT FOUND");
sw.WriteLine();
sw.WriteLine("LOGIN ERROR");
}
}
socket.Disconnect(false); //返回结束 断开该通讯 等待下一次的通讯
}

web服务器:根据url,返回登陆是否成功

 /// <summary>
/// 把url中“?”后面的查询字符串转字典
/// </summary>
/// <param name="url">url://login.html?username=admin&password=123</param>
/// <returns>包含参数的字典</returns>
private static Dictionary<string,string> ParseQueryString(string url) //login.html?username=admin&password=123
{
Dictionary<string, string> dict = new Dictionary<string, string>();
string ss = url.Split('?')[]; //username=admin&password=123
string[] strs = ss.Split('&');
foreach(string str in strs)
{
string[] dds = str.Split('='); //username=admin
dict[dds[]] = dds[];
}
return dict;
}

把url中“?”后面的查询字符串转字典

上一篇:C#学习笔记(八):扩展方法


下一篇:生成ssl证书