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 ...
随机推荐
- Javascript 中childNodes和children的区别
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 01 - 编译链接第一个wxWidgets3.0例子
1. preprocessor #define __WXMSW__#define WXUSINGDLL 2. vc10中设置Include dir, lib dir, dll path VC++平台编 ...
- Libsvm学习
本篇博客转自 http://www.cppblog.com/guijie/archive/2013/09/05/169034.html 在电脑文件夹E:\other\matlab 20 ...
- wxpython 中 用鼠标拖动控件 总结
#encoding: utf-8 import wx import os import noname class Frame( noname.MyFrame1 ): def __init__(self ...
- 在VS2012后的版本中做数据报表时,提示尚未指定报表“Report1”的报表定义
有一群的朋友在用VS2012做数据报表时,老是提示 本地报表处理期间出错. 尚未指定报表“Report1”的报表定义 未将对象引用设置到对象的实例. 我看了一下,步骤没错,我用VS2010做了一下,一 ...
- 90. Subsets II
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- Retrofit所有知识场景汇总
https://futurestud.io/blog/retrofit-getting-started-and-android-client Retrofit Series Overview Gett ...
- 机器学习 —— 概率图模型(Homework: Representation)
前两周的作业主要是关于Factor以及有向图的构造,但是概率图模型中还有一种更强大的武器——双向图(无向图.Markov Network).与有向图不同,双向图可以描述两个var之间相互作用以及联系. ...
- MyEclipse 2014GA 新建 Web Project 并配置 SSH
基本软件配置: 1)MyEclipse 2014GA(JDK:内置 1.7.0.u45:SSH:内置 Struts2.1.Spring3.1 和 Hibernate4.1) 2)apache- ...
- PHP5.4连接sqlserver
1.下载微软的php连接驱动:SQLSRV30.EXE(5.4对应,后面的native client要用2012)/SQLSRV20.EXE(5.3对应,native client要用2008)/SQ ...