屋顶上的木帷幕

海鸥之所以追着渔船飞,是因为它们认为会有沙丁鱼抛向大海 - Eric Cantona, 1995
随笔 - 146, 评论 - 3168, 引用 - 56

导航

工具

关于


标签

每月存档

广告



访客

 

借助http.sys和.NET 2.0中新增的System.Net.HttpListener类,程序员无需借助IIS就能实现自己的Web服务器,而且代码非常简洁。对此,网上已经发布了不少介绍性的文章,例如:《在没有IIS的条件下运行ASMX》(英语原文:“Run ASMX Without IIS”)、VS2005 Beta1, WSE2.0, http.sys and the HTTPListener: Look Ma, NO IIS以及Using Http.Sys to receive messages with WSE 2.0

下面是HttpListener的一个最小的完整例子(需要VS.NET 2005 Beta 2):

using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Threading; using System.IO; namespace MiniHttpsSrv { class Program : IDisposable { static void Main(string[] args) { MiniHttpsSrv.Program server = new Program(); server.Run(); } HttpListener listener = null; public void Run() { this.listener = new HttpListener(); listener.Prefixes.Add("http://+:8000/"); listener.Prefixes.Add("https://+:8001/"); listener.Start(); while(true) { HttpListenerContext context = listener.GetContext(); ThreadPool.QueueUserWorkItem(new WaitCallback(Program.OnGetContext), context); } } public static void OnGetContext(object objContext) { try { HttpListenerContext context = (HttpListenerContext)objContext; Console.WriteLine("(" + Thread.CurrentThread.ManagedThreadId + ")" + DateTime.Now.ToShortTimeString() + " - " + context.Request.Url); context.Response.ContentType = "text/html"; StreamWriter writer = new StreamWriter(context.Response.OutputStream); writer.WriteLine("<html><body><b>It's now " + DateTime.Now + "</b><br/><h1>Hey, I have got your request to <i><u>" + context.Request.Url + "</u></i> </h1></body></html>"); writer.Flush(); context.Response.Close(); } catch (System.Net.HttpListenerException exception) { Console.WriteLine(exception.Message + "\r\n" + exception.StackTrace); } } IDisposable Members } }

从上面的代码可以看到,HttpListener也能够支持HTTPS:

listener.Prefixes.Add("https://+:8001/");

但是要让这行代码工作真正工作起来,需要事先对http.sys进行配置。如何配置,网上也有几篇文章谈及,例如Configure System.Net.HttpListener to listen for SSL以及Simple Secure Indigo (HTTPS),但说得还不够细致。具体来说,要让HttpListener能够接受https请求,需要进行如下的配置:

  1. 前提条件:IIS 6已经安装。下面需要利用IIS 6的mmc(既inetmgr.exe)来帮助倒入证书和察看证书的thumbprint。
     
  2. 按照《按部就班——图解配置IIS5的SSL安全访问》所述步骤,在IIS 6上配置好服务器端证书。这篇Blog极其详细,虽然说的是IIS 5以及Windows 2000上的证书服务,但IIS 6和Windows 2003上的证书服务大同小异,只有界面上的少许差别。配置完成后,可以在inetmgr.exe中到"View Certificate..."察看证书的细节了:
     

     
  3. (这一步可以跳过)用winhttpcertcfg.exe检查确认证书已经被正确的安装了。在命令行运行“winhttpcertcfg.exe -l -c LOCAL_MACHINE\My -s atc-zhengzy”(这里atc-zhengzy是证书的Friendly name),将会看到:
     

     
  4. 运行httpcfg.exe(Windows XP SP2上的httpcfg.exe可以在Microsoft网站下载)将上一步安装好的证书加到http.sys的端口配置中:在命令行运行“httpcfg set ssl -i 0.0.0.0:8001 -c MY -h cd8c7c1958441e53eb4f9f6ba59b271ffb1aa59”,其中-h后面所跟的字符串就是第二步中看到的证书的Thumbprint,-i后面的端口要和代码中HttpListener所监听的https端口一致。如果返回结果“HttpSetServiceConfiguration completed with 0”,表明配置成功。
     
  5. (这步可以跳过)检查确认第四步运行成功。在命令行运行“httpcfg query ssl”,应该看到的结果是:
     

     
  6. 现在就可以运行前面例子代码实现的MiniHttpsSrv了,同时,IIS 6可以停掉,也可以继续运行,不会影响MiniHttpsSrv在8000和8001端口的服务。
     
  7. 测试:IE里面输入https://localhost:8001/blahblah或者http://localhost:8000/blahblah都可以看到echo back的结果:
     

另外,.NET 2.0里面还新增了System.Net.Security.SslStream类,用来实现一个SSL的服务器也很简单,可以在运行时直接从.cer文件中装载证书。


随贴广告(测试期)
相关文章

打印 | 张贴于 2005-07-12 15:11:00 | Tag:Dot NET

留言反馈

re: 配置HttpListener侦听SSL连接详解 编辑
修改你的页面,似乎你的页面把首页撑大了:(
2005-07-20 02:51:00 | [匿名用户:开心就好]
re: 配置HttpListener侦听SSL连接详解 编辑
偶像啊,文章写的通俗易懂。
真是不容易,我一直想要连成这种功力,无奈学识浅薄。
佩服佩服
2005-07-13 16:49:00 | [匿名用户:daniel]
re: 配置HttpListener侦听SSL连接详解 编辑
http.sys要求windows xp sp2或者windows 2003。

@cookey:效率肯定不如IIS的。但是,HttpListener的目的不是取代IIS。HttpListener是提供了一种very light weight的HTTP server的实现途径。
2005-07-12 16:36:00 | [匿名用户:mvm]
re: 配置HttpListener侦听SSL连接详解 编辑
是不是不能在Windows2000下使用啊
2005-07-12 16:25:00 | [匿名用户:LIRENZHAO]
re: 配置HttpListener侦听SSL连接详解 编辑
效率怎么样啊。如果优化后效率还不如IIS,感觉也属于没有用的功能了.
2005-07-12 15:52:00 | [匿名用户:cookey]
博客主人设置本博客不允许匿名用户发表言论,请登录后再试

Powered by: Joycode MVC Blogger System