用 C# 自己動(dòng)手編寫一個(gè) Web 服務(wù)器
在.NET世界中,C#是一種功能強(qiáng)大的編程語(yǔ)言,常被用于構(gòu)建各種類型的應(yīng)用程序,包括Web服務(wù)器。雖然在實(shí)際生產(chǎn)環(huán)境中,我們通常會(huì)使用成熟的Web服務(wù)器軟件(如IIS、Kestrel等),但了解如何用C#從頭開始構(gòu)建一個(gè)簡(jiǎn)單的Web服務(wù)器,對(duì)于深入理解HTTP協(xié)議和網(wǎng)絡(luò)編程是非常有價(jià)值的。
本文將指導(dǎo)你使用C#編寫一個(gè)簡(jiǎn)單的Web服務(wù)器,并包含具體的代碼實(shí)現(xiàn)。
第一步:理解HTTP協(xié)議
在編寫Web服務(wù)器之前,我們需要對(duì)HTTP協(xié)議有一個(gè)基本的了解。HTTP是一種無(wú)狀態(tài)的、基于請(qǐng)求和響應(yīng)的協(xié)議??蛻舳耍ㄈ鏦eb瀏覽器)發(fā)送HTTP請(qǐng)求到服務(wù)器,服務(wù)器處理請(qǐng)求并返回HTTP響應(yīng)。
HTTP請(qǐng)求由請(qǐng)求行、請(qǐng)求頭部和請(qǐng)求體組成。請(qǐng)求行包含請(qǐng)求方法(GET、POST等)、請(qǐng)求URL和HTTP協(xié)議版本。請(qǐng)求頭部包含關(guān)于請(qǐng)求的附加信息,如Host、User-Agent等。請(qǐng)求體包含實(shí)際發(fā)送給服務(wù)器的數(shù)據(jù),通常用于POST請(qǐng)求。
HTTP響應(yīng)由狀態(tài)行、響應(yīng)頭部和響應(yīng)體組成。狀態(tài)行包含HTTP協(xié)議版本、狀態(tài)碼和狀態(tài)消息。響應(yīng)頭部包含關(guān)于響應(yīng)的附加信息,如Content-Type、Content-Length等。響應(yīng)體包含服務(wù)器返回給客戶端的實(shí)際數(shù)據(jù)。
第二步:創(chuàng)建TCP監(jiān)聽器
在C#中,我們可以使用TcpListener類來(lái)創(chuàng)建一個(gè)TCP監(jiān)聽器,用于監(jiān)聽傳入的HTTP請(qǐng)求。以下是一個(gè)簡(jiǎn)單的示例代碼,展示如何創(chuàng)建TCP監(jiān)聽器并等待連接:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
class SimpleWebServer
{
private const int Port = 8080;
public static void Main()
{
TcpListener listener = new TcpListener(IPAddress.Any, Port);
listener.Start();
Console.WriteLine($"Server started at http://localhost:{Port}/");
while (true)
{
TcpClient client = listener.AcceptTcpClient();
HandleClientAsync(client).Wait();
}
}
private static async Task HandleClientAsync(TcpClient client)
{
NetworkStream stream = client.GetStream();
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
StreamWriter writer = new StreamWriter(stream, Encoding.UTF8) { AutoFlush = true };
try
{
// 讀取請(qǐng)求行
string requestLine = await reader.ReadLineAsync();
if (string.IsNullOrEmpty(requestLine))
return;
Console.WriteLine($"Received request: {requestLine}");
// 解析請(qǐng)求行(為了簡(jiǎn)化,這里只處理GET請(qǐng)求)
string[] parts = requestLine.Split(' ');
if (parts.Length != 3 || parts[0] != "GET")
{
SendErrorResponse(writer, 400, "Bad Request");
return;
}
string path = parts[1];
if (path != "/")
{
SendErrorResponse(writer, 404, "Not Found");
return;
}
// 發(fā)送響應(yīng)
SendResponse(writer, 200, "OK", "<html><body><h1>Hello, World!</h1></body></html>");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
SendErrorResponse(writer, 500, "Internal Server Error");
}
finally
{
client.Close();
}
}
private static void SendResponse(StreamWriter writer, int statusCode, string statusMessage, string content)
{
writer.WriteLine($"HTTP/1.1 {statusCode} {statusMessage}");
writer.WriteLine("Content-Type: text/html; charset=UTF-8");
writer.WriteLine($"Content-Length: {content.Length}");
writer.WriteLine();
writer.Write(content);
}
private static void SendErrorResponse(StreamWriter writer, int statusCode, string statusMessage)
{
string content = $"<html><body><h1>{statusCode} {statusMessage}</h1></body></html>";
SendResponse(writer, statusCode, statusMessage, content);
}
}
這個(gè)示例代碼創(chuàng)建了一個(gè)簡(jiǎn)單的Web服務(wù)器,監(jiān)聽8080端口。當(dāng)客戶端連接到服務(wù)器時(shí),服務(wù)器會(huì)讀取請(qǐng)求行,并根據(jù)請(qǐng)求路徑返回相應(yīng)的響應(yīng)。