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中“?”后面的查询字符串转字典
ASP.NET CORE Web浏览器和Web服务器的更多相关文章
- 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 创 ...
- ASP.NET Core MVC中构建Web API
在ASP.NET CORE MVC中,Web API是其中一个功能子集,可以直接使用MVC的特性及路由等功能. 在成功构建 ASP.NET CORE MVC项目之后,选中解决方案,先填加一个API的文 ...
- Web浏览器与Web服务器之间的通信过程
HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤:1:建立TCP连接 在HTTP工作开始之前,Web浏览器首先要通过网络与Web服务器建立连接,该连 ...
- http通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤
http通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: (1) 建立TCP连接 在HTTP工作开始之前,Web浏览器首先要通过网络与Web服务器建立连接,该连接是通过TCP来完成 ...
- asp.net core 3.0获取web应用的根目录
目录 1.需求 2.解决方案 1.需求 asp.net core 3.0的web项目中,在controller中,想要获取wwwroot下的imgs/banners文件夹下的所有文件: 在传统的asp ...
- ASP.NET Core 网站发布到Linux服务器(转)
出处;ASP.NET Core 网站发布到Linux服务器 长期以来,使用.NET开发的应用只能运行在Windows平台上面,而目前国内蓬勃发展的互联网公司由于成本的考虑,大量使用免费的Linux平台 ...
- ASP.NET Core 入门教程 2、使用ASP.NET Core MVC框架构建Web应用
一.前言 1.本文主要内容 使用dotnet cli创建基于解决方案(sln+csproj)的项目 使用Visual Studio Code开发基于解决方案(sln+csproj)的项目 Visual ...
- 初探ASP.NET Core 3.x (3) - Web的运作流程和ASP.NET Core的运作结构
本文地址:https://www.cnblogs.com/oberon-zjt0806/p/12215717.html 注意:本篇大量地使用了mermaid绘制图表,加载需要较长的时间,请见谅 [TO ...
- ASP.NET Core 1.0开发Web API程序
.NET Core版本:1.0.0-rc2Visual Studio版本:Microsoft Visual Studio Community 2015 Update 2开发及运行平台:Windows ...
随机推荐
- POJ 2080 Calendar(很水的模拟)
刚开始一直WA,才发现原来代码中两处减去年份.月份的天数的判断条件用的是>=,虽然最后考虑n=0要退回一天的情况,但还是WA.后来改成>的条件判断,省去了考虑n=0的麻烦,AC. 此题无非 ...
- 深入浅出ES6(五):不定参数和默认参数
作者 Jason Orendorff github主页 https://github.com/jorendorff 不定参数 我们通常使用可变参函数来构造API,可变参函数可接受任意数量的参数.例 ...
- 让IE系列浏览器支持HTML5(share)
引用Google的html5.js文件 <!--[if IE]> <script src=”http://html5shiv.googlecode.com/svn/trunk/htm ...
- ubuntu12.10+NDK r9 编译 ffmpeg 的一些参考资料Perhaps you should add the directory containing `libssl.pc'
首先入门级的 编译宝典: https://trac.ffmpeg.org/wiki/CompilationGuide/Android http://www.roman10.net/how-to-bui ...
- lintcode :reverse integer 颠倒整数
题目: 颠倒整数 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 解题: 直接 ...
- iOS开发--数组
1.sortedArrayUsingSelector (按Key值大小对NSDictionary排序) NSMutableArray *array = [NSMutableArray arrayWit ...
- 33. Search in Rotated Sorted Array
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- java 调用 phantomjs
java 调用 phantomjs 2014-11-21 13:55 2034人阅读 评论(2) 收藏 举报 分类: phantomjs(2) 日前有采集需求,当我把所有的对应页面的链接都拿到手, ...
- android的jni
一.底层实现: c文件:hardware/libhardware_legacy/power/power.c 以其中set_screen_state(int)函数为例 其Android.mk中添加: ...
- 《Linux/Unix系统编程手册》读书笔记1
<Linux/Unix系统编程手册>读书笔记 目录 最近这一个月在看<Linux/Unix系统编程手册>,在学习关于Linux的系统编程.之前学习Linux的时候就打算写关于L ...