使用IIS建立自己的网站、使用C#编写IIS模拟器,更好的理解Client和Server的relation

时间:2024-01-01 18:56:27

如何在IIS服务器上搭建自己的网站呢,今天带着这问题进行简单的实践一下,并且准备模拟一下IIS服务器工作方式,把这个工作方式搞清楚有利于发展。

1、首先应该进入控制面板=》程序=》添加或删除程序=》找到IIS 全部打钩。

  

2、

使用IIS建立自己的网站、使用C#编写IIS模拟器,更好的理解Client和Server的relation

2、首先要自己建立一个网页。进行模拟。

使用IIS建立自己的网站、使用C#编写IIS模拟器,更好的理解Client和Server的relation

3、然后在打开的IIS服务器,点击右键进行建立网页。

使用IIS建立自己的网站、使用C#编写IIS模拟器,更好的理解Client和Server的relation

4、然后添加各种配置,如图:

使用IIS建立自己的网站、使用C#编写IIS模拟器,更好的理解Client和Server的relation

5、填写必须的参数如图:

使用IIS建立自己的网站、使用C#编写IIS模拟器,更好的理解Client和Server的relation

6、点击OK,点击浏览,如果端口是80端口。不会弹出你所希望看到的窗口。

使用IIS建立自己的网站、使用C#编写IIS模拟器,更好的理解Client和Server的relation

使用IIS建立自己的网站、使用C#编写IIS模拟器,更好的理解Client和Server的relation

7、所以我们要进行简单的调试。

使用IIS建立自己的网站、使用C#编写IIS模拟器,更好的理解Client和Server的relation

8、点击绑定按钮:

使用IIS建立自己的网站、使用C#编写IIS模拟器,更好的理解Client和Server的relation

9:再次在浏览器上查看;

使用IIS建立自己的网站、使用C#编写IIS模拟器,更好的理解Client和Server的relation

到这里一个IIS上布置就基本上完成了。由于时间关系模拟IIS服务器下次给出详细的代码,有助你更好的学习网站编程。

先上一个总体的分析图:

使用IIS建立自己的网站、使用C#编写IIS模拟器,更好的理解Client和Server的relation

IISDemo

这里给给关注我的好友道个歉,说是补齐下半部分,已经时隔半年,今天终于补齐了这文章,补齐这个文章也引起了我的深思,到你因该怎样学习,才能成为出色的web开发者。

IIS模拟思路:

      1. 使用Socket套接字编程。

2. 深刻理解web Form 中的 IhttpHandler 、HttpApplication、HttpContext、HttpRequest、HttpResponse 几个封装的类。

3. 对http协议的有个深刻的认识,或者理解其协议格式。

4. 最后就是不错的编程基础。把想法写成代码。

socket编程

socket编程主要有一下几步

       //获取ip和端口
string host = txthost.Text.Trim();
int port =int.Parse(txtPort.Text.Trim());
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(host), port);
//建立socket链接
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(ipe);
s.Listen(10);
txtbody.Text = "开始监听";
//开始接收,接收时会阻塞,所以采用多进程的方式
System.Threading.Thread td = new Thread(()=>
{
while(true)
{
Socket temp = s.Accept();//为新建立的socket txtbody.Text += "\r\n建立链接";
string recvStr =string.Empty;//创建接受字符串
byte[] recvBytes = new byte[2*1024*1024];
int len= temp.Receive(recvBytes, recvBytes.Length, 0); //获取前台取得的字符串
string str = System.Text.Encoding.Default.GetString(recvBytes, 0, len); //解析报文 HttpContext context = new HttpContext(str); //处理请求 HttpApplication application = new HttpApplication();
application.processContext(context); //返回响应 temp.Send(context.response.responseHeader()); temp.Send(context.response.responsebody); //关闭socket
temp.Shutdown(SocketShutdown.Both);
temp.Close(); } });
td.Start();

  

HttpContext

字面理解是http上下文。暂且不用去管ms封住的,这里为了演示方面里面封装了httpRequest,httpResponse属性。

 public  class HttpContext
{
public HttpRequest request { get; set; }
public HttpResponse response { get; set; }
public HttpContext(string requestStr)
{
request = new HttpRequest(requestStr);
response = new HttpResponse(request);
}
}

HttpRequest

httpRequest 用于解析Client端的请求,从而告诉context上下文,服务器应该返回怎样的数据给Client端。

  public class HttpRequest
{
public string getMothed { get; set; }
public string url { get; set; }
public string version { get; set; }
public HttpRequest( string requestStr)
{
//GET http://c.gj.qq.com HTTP/1.1
//Host: c.gj.qq.com
//Connection: keep-alive
//Accept: image/webp,image/*,*/*;q=0.8
//User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36
//Accept-Encoding: gzip, deflate, sdch
//Accept-Language: zh-CN,zh;q=0.8
//Cookie: pgv_pvid=9224263521; o_cookie=871119239; ptui_loginuin=871119239@qq.com; ptcz=4308290a6e8e29b0645417e52d73567b0bc7ba24b13508b2c685c2a7ccaaec3f; pt2gguin=o0871119239; uin=o0871119239; skey=@Tk5Xvq7l1; qm_username=871119239; qm_sid=0921d209c7585c27d505c5d093cc1d22,qblA2Z21rc3F1eFZBZjFvczRwVypoZXhXelpsU3dQdS0wa2tJQ0l5eWNkSV8.
//解析前台发送回来的数据
var allLines = requestStr.Replace("\r\n", "\r");
//获取第一行
var firstline = allLines.Split('\r')[];
//获取请求的方法
getMothed = firstline.Split(' ')[];
url = firstline.Split(' ')[];
version = firstline.Split(' ')[];
}
}

HttpResponse

httpResponse是返回响应的请求。着返回响应的求请求,必须要处理就需要httpApplicationl.

  public byte[] responsebody { get; set; }
public HttpRequest request { get; set; }
public HttpResponse(HttpRequest Request)
{
request = Request;
}
public byte[] responseHeader()
{
//HTTP/1.1 200 OK
//Date: Sun, 08 May 2016 05:55:33 GMT
//Server: Apache
//Cache-Control: max-age=0
//Expires: Sun, 08 May 2016 05:55:33 GMT
//Vary: Accept-Encoding
//Content-Length: 40
//Connection: close
//Content-Type: text/html
StringBuilder sb = new StringBuilder();
sb.AppendFormat("HTTP/1.1 200 OK\r\n");
sb.AppendFormat("Content-Length:{0}\r\n",responsebody.Length);
//获取请求类型
var ext=System.IO.Path.GetExtension(request.url);
sb.AppendFormat("Content-Type:{0}\r\n\r\n", GetContenType(ext));
return System.Text.Encoding.Default.GetBytes(sb.ToString()); }
public string GetContenType(string ext)
{
string type = "text/html";
switch (ext)
{
case ".aspx":
case ".html":
case ".htm":
type = "text/html";
break;
case ".png":
type = "image/png";
break;
case ".gif":
type = "image/gif";
break;
case ".jpg":
case ".jpeg":
type = "image/jpeg";
break;
case ".css":
type = "text/css";
break;
case ".js":
type = "application/x-javascript";
break;
default:
type = "text/plain";
break;
}
return type;
}
}

HttpApplication

该类继承接口IhttpHandler

   public interface IhttpHandler
{
void processContext(HttpContext context);
}
public class HttpApplication:IhttpHandler
{
public void processContext(HttpContext context)
{
//拼接路径
string basePath = AppDomain.CurrentDomain.BaseDirectory;
//虚拟路径转绝对路径
string urlPath = context.request.url.TrimStart('/');
string path = System.IO.Path.Combine(basePath, urlPath);
if (System.IO.File.Exists(path))
{
byte[] b = System.IO.File.ReadAllBytes(path);
context.response.responsebody = b;
}
else
{
byte[] b = System.IO.File.ReadAllBytes(basePath +@"/404.html");
context.response.responsebody = b;
} }
}

运行程序即可得到上述发布的网页。