HttpListener的幾種用法

2022-02-20 08:31:51 字數 3069 閱讀 9268

簡單的httplistener 示例

1

public

static

void

newmethod1()2/

", 8888)); //

新增需要監聽的url範圍

5 listener.start(); //

開始監聽埠,接收客戶端請求

6 console.writeline("

listening...");

78//阻塞主函式至接收到乙個客戶端請求為止

對客戶端輸出相應資訊.

16 response.contentlength64 =buffer.length;

17 system.io.stream output =response.outputstream;

18 output.write(buffer, 0

, buffer.length);

19//

關閉輸出流,釋放相應資源

20console.writeline(encoding.default.getstring(buffer));

21console.readkey();

22output.close();

2324 }

處理多使用者請求

1

//客戶請求處理

關閉輸出流,釋放相應資源

16output.close();17}

1819

public

static

void

newmethod2()

2035}36

catch

(exception e)

3741

finally

4245 }

實現重定向

1

public

static

void

newmethod3()

2

1

static

void method4(object

obj)218

19public

static

void

newmethod4()

2035}36

catch

(exception e)

3741

finally

4245 }

斷點續傳

在未寫完所有檔案時,如果客戶端關閉連線,會拋此異常

4246}47

48static

void

copystream(stream orgstream, stream desstream)

4959}60

public

static

void

newmethod5()

6176}77

catch

(exception e)

7882

finally

8386 }

基於HttpListener的web伺服器

前面兩篇文章分別介紹了基於原始socket的web伺服器和基於tcplistener的web伺服器,本篇文章將繼續介紹另外一種基於httplistener的。httplistener進一步的簡化了http協議的監聽,僅需通過字串的方法提供監聽的位址和埠號以及虛擬路徑,就可以開始監聽工作了。設定字首,...

c 使用HttpListener監聽HTTP請求

最近在專案上需要與第三方系統對接,對方會通過http請求定時推送資料,因此需要在專案中新增監聽http請求的功能,查閱了相關資料,使用system.net下的httplistener實現此功能。基本變數private listeneruri private httplistener listener...

用HttpListener實現檔案斷點續傳

普通方式請求伺服器上的乙個文時,所發出的請求和接受到的伺服器如下 request header cache control no cache connection close pragma no cache accept host localhost response header 當伺服器支援斷點...