//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中“?”后面的查询字符串转字典

ASP.NET CORE Web浏览器和Web服务器的更多相关文章

  1. 002.Create a web API with ASP.NET Core MVC and Visual Studio for Windows -- 【在windows上用vs与asp.net core mvc 创建一个 web api 程序】

    Create a web API with ASP.NET Core MVC and Visual Studio for Windows 在windows上用vs与asp.net core mvc 创 ...

  2. ASP.NET Core MVC中构建Web API

    在ASP.NET CORE MVC中,Web API是其中一个功能子集,可以直接使用MVC的特性及路由等功能. 在成功构建 ASP.NET CORE MVC项目之后,选中解决方案,先填加一个API的文 ...

  3. Web浏览器与Web服务器之间的通信过程

     HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤:1:建立TCP连接 在HTTP工作开始之前,Web浏览器首先要通过网络与Web服务器建立连接,该连 ...

  4. http通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤

    http通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: (1)    建立TCP连接 在HTTP工作开始之前,Web浏览器首先要通过网络与Web服务器建立连接,该连接是通过TCP来完成 ...

  5. asp.net core 3.0获取web应用的根目录

    目录 1.需求 2.解决方案 1.需求 asp.net core 3.0的web项目中,在controller中,想要获取wwwroot下的imgs/banners文件夹下的所有文件: 在传统的asp ...

  6. ASP.NET Core 网站发布到Linux服务器(转)

    出处;ASP.NET Core 网站发布到Linux服务器 长期以来,使用.NET开发的应用只能运行在Windows平台上面,而目前国内蓬勃发展的互联网公司由于成本的考虑,大量使用免费的Linux平台 ...

  7. ASP.NET Core 入门教程 2、使用ASP.NET Core MVC框架构建Web应用

    一.前言 1.本文主要内容 使用dotnet cli创建基于解决方案(sln+csproj)的项目 使用Visual Studio Code开发基于解决方案(sln+csproj)的项目 Visual ...

  8. 初探ASP.NET Core 3.x (3) - Web的运作流程和ASP.NET Core的运作结构

    本文地址:https://www.cnblogs.com/oberon-zjt0806/p/12215717.html 注意:本篇大量地使用了mermaid绘制图表,加载需要较长的时间,请见谅 [TO ...

  9. ASP.NET Core 1.0开发Web API程序

    .NET Core版本:1.0.0-rc2Visual Studio版本:Microsoft Visual Studio Community 2015 Update 2开发及运行平台:Windows ...

随机推荐

  1. Nagios 安装及微信短信提醒

    引言 Nagios 作为业界非常强大的一款开源监视系统. 监控网络服务(SMTP.POP3.HTTP.NNTP.PING 等): 监控主机资源(处理器负荷.磁盘利用率等): 简单地插件设计使得用户可以 ...

  2. ubuntu下安装spark1.4.0

    构建在hadoop2.6.0之上的 1.在官网下载spark-1.4.0-bin-hadoop2.6.tgz 2.解压到你想要放的文件夹里,tar zxvf spark-1.4.0-bin-hadoo ...

  3. Strange java.lang.ArrayIndexOutOfBoundsException thrown on jetty startup

    http://stackoverflow.com/questions/26496338/strange-java-lang-arrayindexoutofboundsexception-thrown- ...

  4. MySQL 语句级避免重复插入—— Insert Select Not Exist

    想要插入一条数据,要避免重复插入,又不想折腾两回数据库连接操作,可以参考如下办法. INSERT INTO table(column1,column2,column3 ...columnN) SELE ...

  5. ActiveMQ 学习笔记

    http://somebody-hjh.iteye.com/blog/726050 一.概述 Message,即消息.人与人之间通过消息传递信息.言语.眼神.肢体动作都可被视为消息体.当然还有我们经常 ...

  6. lintcode: 翻转链表

    题目: 翻转链表 翻转一个链表 样例 给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null 挑战 在原地一次翻转完成 解题: 递归还 ...

  7. untiy 插件工具: 游戏中 策划数据Excel 导出到项目中

    https://github.com/zhutaorun/Excel2Unity,这个项目是直接下载就可以用的, 其中原理和相关的解释 http://blog.csdn.net/neil3d/arti ...

  8. Java Logger(java日志)

    目录 1. 简介2. 安装3. log4j基本概念3.1. Logger3.2. Appender3.2.1. 使用ConsoleAppender3.2.2. 使用FileAppender3.2.3. ...

  9. 关于Linux的时间与时区

    转:http://linux.chinaunix.net/techdoc/beginner/2007/06/22/960790.shtml 首先要说明的是我的系统是fedora,其他系统可能不完全相同 ...

  10. Mmap的实现原理和应用

    http://blog.csdn.net/edwardlulinux/article/details/8604400 很多文章分析了mmap的实现原理.从代码的逻辑来分析,总是觉没有把mmap后读写映 ...