//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. android.os.DeadObjectException memory near r0: 异常处理 Consumer closed input channel or an error occurred. events=0x9

    原地址:http://www.cnblogs.com/wanqieddy/p/3495338.html android.os.DeadObjectException memory near r0: 异 ...

  2. 山寨小小军团开发笔记 之 Arrow Projectile

    好久没怎么更新博客了,今天抽空来一篇,讨论一下弓箭的轨迹生成. 一.原理 弓箭的轨迹本质就是一个数学问题,使用一个 bezier 曲线公式就可以插值生成.得到轨迹后,做一个lookAt就可以了. 二. ...

  3. Sublime Text 3配置与vim模式(待完整)

    Sublime Text 3通过设置默认值与用户值的方式,来进行配置.默认值不允许更改,用户值是用户进行配置.同一属性,当用户值存在时,默认值就无效.打开Preference,如图: 先贴下我的Set ...

  4. 解决NTFS分区上的代码在linux上编译后没有权限执行

    win7下的cpp代码,在ubuntu下编译后,可执行文件不能执行,root也不行. 将代码拷贝到ubuntu上,再编译生成的可执行文件则可以执行.或者将win7分区上的可执行文件拷贝出来,然后chm ...

  5. Python Snippet

    python按行读取文件,如何去掉换行符"\n" for line in file.readlines(): line=line.strip('\n') python没有subst ...

  6. Web 技术人员需知的Web 缓存知识

    最近的译文距今已有4年之久,原文有一定的更新.今天踩着前辈们的肩膀,再次把这篇文章翻译整理下.一来让自己对web缓存的理解更深刻些,二来让大家注意力稍稍转移下,不要整天HTML5, 面试题啊叨啊叨的~ ...

  7. Hibernate逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  8. 配置Excel的DCOM权限

    异常详细信息: System.UnauthorizedAccessException: 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-00000000004 ...

  9. android从应用到驱动之—camera(1)---程序调用流程

    一.开篇 写博客还得写开篇介绍,可惜,这个不是我所擅长的.就按我自己的想法写吧. 话说camera模块,从上层到底层一共包含着这么几个部分: 1.apk------java语言 2.camera的ja ...

  10. Topcoder 练习小记,Java 与 Python 分别实现。

    Topcoder上的一道题目,题目描述如下: Problem Statement      Byteland is a city with many skyscrapers, so it's a pe ...