程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#實現一個簡單的HTTP服務器

C#實現一個簡單的HTTP服務器

編輯:C#入門知識

用到的類主要有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:不進行身份驗證。

      

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved