主頁 > 知識庫 > C#實現(xiàn)HTTP協(xié)議迷你服務(wù)器(兩種方法)

C#實現(xiàn)HTTP協(xié)議迷你服務(wù)器(兩種方法)

熱門標(biāo)簽:服務(wù)器配置 銀行業(yè)務(wù) 電子圍欄 Mysql連接數(shù)設(shè)置 Linux服務(wù)器 科大訊飛語音識別系統(tǒng) 阿里云 團(tuán)購網(wǎng)站
本文以兩種稍微有差別的方式用C#語言實現(xiàn)HTTP協(xié)議的服務(wù)器類,之所以寫這些,也是為了自己能更深刻了解HTTP底層運作。

要完成高性能的Web服務(wù)功能,通常都是需要寫入到服務(wù),如IIS,Apache Tomcat,但是眾所周知的Web服務(wù)器配置的復(fù)雜性,如果我們只是需要一些簡單的功能,安裝這些組件看起來就沒多大必要。我們需要的是一個簡單的HTTP類,可以很容易地嵌入到簡單的Web請求的服務(wù),加到自己的程序里。

實現(xiàn)方法一
.net框架下有一個簡單但很強(qiáng)大的類HttpListener。這個類幾行代碼就能完成一個簡單的服務(wù)器功能。雖然以下內(nèi)容在實際運行中幾乎毫無價值,但是也不失為理解HTTP請求過程的細(xì)節(jié)原理的好途徑。
復(fù)制代碼 代碼如下:

HttpListener httpListener = new HttpListener();
httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
httpListener.Prefixes.Add("http://localhost:8080/");
httpListener.Start();
new Thread(new ThreadStart(delegate
{
while (true)
{
HttpListenerContext httpListenerContext = httpListener.GetContext();
httpListenerContext.Response.StatusCode = 200;
using (StreamWriter writer = new StreamWriter(httpListenerContext.Response.OutputStream))
{
writer.WriteLine("html>head>meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>title>測試服務(wù)器/title>/head>body>");
writer.WriteLine("div style=\"height:20px;color:blue;text-align:center;\">p> hello/p>/div>");
writer.WriteLine("ul>");
writer.WriteLine("/ul>");
writer.WriteLine("/body>/html>");
}
}
})).Start();

如果你運用的好,舉一反三的話,這樣一個簡單的類可能會收到意想不到的效果,但是由于HttpListener這個類對底層的完美封裝,導(dǎo)致對協(xié)議的控制失去靈活性,因此我想大型服務(wù)器程序里肯定不會用這個類去實現(xiàn)。因此如果必要的話,自己用Tcp協(xié)議再去封裝一個類,以便更好的控制服務(wù)運行狀態(tài)。

實現(xiàn)方法二:
這個方法是一個老外分享的,雖然文件內(nèi)容看起來很亂,其實邏輯很強(qiáng)很有條理。讓我們來分析一下實現(xiàn)過程:
通過子類化HttpServer和兩個抽象方法handlegetrequest和handlepostrequest提供實現(xiàn)…
復(fù)制代碼 代碼如下:

public class MyHttpServer : HttpServer {
public MyHttpServer(int port)
: base(port) {
}
public override void handleGETRequest(HttpProcessor p) {
Console.WriteLine("request: {0}", p.http_url);
p.writeSuccess();
p.outputStream.WriteLine("html>body>h1>test server/h1>");
p.outputStream.WriteLine("Current Time: " + DateTime.Now.ToString());
p.outputStream.WriteLine("url : {0}", p.http_url);
p.outputStream.WriteLine("form method=post action=/form>");
p.outputStream.WriteLine("input type=text name=foo value=foovalue>");
p.outputStream.WriteLine("input type=submit name=bar value=barvalue>");
p.outputStream.WriteLine("/form>");
}
public override void handlePOSTRequest(HttpProcessor p, StreamReader inputData) {
Console.WriteLine("POST request: {0}", p.http_url);
string data = inputData.ReadToEnd();
p.outputStream.WriteLine("html>body>h1>test server/h1>");
p.outputStream.WriteLine("a href=/test>return/a>p>");
p.outputStream.WriteLine("postbody: pre>{0}/pre>", data);
}
}

如果你能夠順利編譯和運行附件中的項目,就你應(yīng)該能夠用Web瀏覽器輸入Http://localhost:8080/,打開就可以看上面的簡單的HTML頁面渲染。讓我們看看怎么具體是怎么實現(xiàn)的吧~!

這個簡單的Web服務(wù)器可以分解為兩個部分。HttpServer類開啟了一個指定輸入端口的TcpListener實例,使用accepttcpclient()用于循環(huán)處理傳入的TCP連接請求。這是處理傳入的TCP連接的第一步。當(dāng)傳入的請求到達(dá)已知的指定端口,這個接受過程會創(chuàng)建一個新的服務(wù)器與客戶端端口配對,用于服務(wù)器語言客戶端的連接通信。
復(fù)制代碼 代碼如下:

View Code
public abstract class HttpServer {
protected int port;
TcpListener listener;
bool is_active = true;
public HttpServer(int port) {
this.port = port;
}
public void listen() {
listener = new TcpListener(port);
listener.Start();
while (is_active) {
TcpClient s = listener.AcceptTcpClient();
HttpProcessor processor = new HttpProcessor(s, this);
Thread thread = new Thread(new ThreadStart(processor.process));
thread.Start();
Thread.Sleep(1);
}
}
public abstract void handleGETRequest(HttpProcessor p);
public abstract void handlePOSTRequest(HttpProcessor p, StreamReader inputData);
}

這樣一些介紹方式可能會讓人產(chǎn)生一頭霧水的感覺,或許直觀地看代碼或調(diào)試示例源代碼程序可能會更容易理解一些。下面就把源碼貼上來弓大家參考,希望能對大家有所幫助!
點擊下載
您可能感興趣的文章:
  • HTTP協(xié)議下用Web Service上傳大文件的解決方案
  • http協(xié)議詳解(超詳細(xì))
  • php獲取通過http協(xié)議post提交過來xml數(shù)據(jù)及解析xml
  • 基于JAVA中Jersey處理Http協(xié)議中的Multipart的詳解
  • javaweb中Http協(xié)議詳解

標(biāo)簽:蚌埠 棗莊 萍鄉(xiāng) 衡水 衢州 廣元 江蘇 大理

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《C#實現(xiàn)HTTP協(xié)議迷你服務(wù)器(兩種方法)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266