您的位置:首页 > 理论基础 > 计算机网络

C#实现一个简单的HTTP服务器

2008-08-04 13:32 633 查看
用到的类主要有HttpListener、StreamWriter.

HttpListener:使用HttpListener可创建响应 HTTP 请求的简单 HTTP 协议侦听器。实际上HttpListener只是实现了服务器端Socket上面的一个简单封装类。通过设置Prefixes属性来进行侦听,如,侦听器绑定到http或https端点的URL(如下代码).侦听器默认是绑定到运行在http的80端口和https的443的端口,并允许匿名和无身份验证的客户端访问。可以使用HttpListener类的属性Prefixes来制定自定义url和端口,并通过设置属性AuthenticationSchemes属性来配置身份验证。

建立HttpListener后,执行Start(执行是Start实际上是引发底层的Bind和Listener),就开始处理客服端输入请求。HttpListener中GetContext和套接字的Accept类似,它在没有请求准备好处理的时候处于被阻塞状态。GetContext返回一个对象HttpListenerContext。HttpListenerContext对象的属性Request属性提供了许多关于客户机的一些请求信息.Response则返回一个HttpListerResponse对象,该对象可以用来响应客户机的请求。

代码如下:using System;
using System.Collections.Generic;
using System.Text;

using System.Net;
using System.Net.Sockets;
using System.IO;

namespace ConsoleHttpEchoServer
{
class Program
{
static void Main(string[] args)
{
using (HttpListener listerner = new HttpListener())
{
listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 Anonymous匿名访问
listerner.Prefixes.Add("http://localhost:8080/web/");

// listerner.Prefixes.Add("http://localhost/web/");
listerner.Start();
Console.WriteLine("WebServer Start Successed.......");
while (true)
{
//等待请求连接
//没有请求则GetContext处于阻塞状态
HttpListenerContext ctx = listerner.GetContext();
ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码
string name = ctx.Request.QueryString["name"];

if (name != null)
{
Console.WriteLine(name);
}

//使用Writer输出http响应代码
using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream))
{
Console.WriteLine("hello");
writer.WriteLine("<html><head><title>The WebServer Test</title></head><body>");
writer.WriteLine("<div style=/"height:20px;color:blue;text-align:center;/"><p> hello {0}</p></div>", name);
writer.WriteLine("<ul>");

foreach (string header in ctx.Request.Headers.Keys)
{
writer.WriteLine("<li><b>{0}:</b>{1}</li>", header, ctx.Request.Headers[header]);

}
writer.WriteLine("</ul>");
writer.WriteLine("</body></html>");

writer.Close();
ctx.Response.Close();
}

}
listerner.Stop();
}
}
}
}

在浏览器 中输入 http://localhost:8080/web/?name=test或 http://localhost/web/?name=test,在浏览器就会出现Hello test 和一些Request头部相关信息.

备注:

1.URI 前缀字符串由方案(http 或 https)、主机、可选端口和可选路径组成。完整前缀字符串的示例为“http://www.contoso.com:8080/customerData/”。前缀必须以正斜杠(“/”)结尾。带有与所请求的 URI 最近似匹配的前缀的 HttpListener 对象响应请求。多个 HttpListener 对象不能添加同一前缀;如果 HttpListener 添加已经使用的前缀,将引发Win32Exception 异常。

2.如果指定了端口,主机元素可以被替换为“*”,以指示如果请求的 URI 与任何其他前缀都不匹配,则 HttpListener 接受发送到该端口的请求。例如,当任何 HttpListener 都不处理请求的 URI 时,若要接收发送到端口 8080 的所有请求,前缀应为“http://*:8080/”。同样,若要指定 HttpListener 接受发送到端口的所有请求,请将主机元素替换为“+”字符,即“https://+:8080”。“*”和“+”字符可出现在包含路径的前缀中。

3.AuthenticationSchemes枚举类有以下值

Anonymous:默认值,允许任意的客户机进行连接,不需要身份验证。

Basic:要求提供纯文本(64位编码)用户名和密码来进行验证。

Digest:类似与基本身份验证,但是用户名和密码使用一个简单密码散列进行传输。

Ntlm: 指定 NTLM 身份验证(只有IE支持)。

IntegratedWindowsAuthentication:指定Windows身份验证。

Negotiate: 和客户端协商以确定身份验证方案。如果客户端和服务器均支持 Kerberos,则使用 Kerberos;否则使用 Ntlm

None:不进行身份验证。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: