今天就跟大家聊聊有關(guān).NET中怎么利用HttpListener實(shí)現(xiàn)Http服務(wù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
1.創(chuàng)建一個(gè)HTTP偵聽(tīng)器對(duì)象并初始化
2.添加需要監(jiān)聽(tīng)的URI 前綴
3.開(kāi)始偵聽(tīng)來(lái)自客戶(hù)端的請(qǐng)求
4.處理客戶(hù)端的Http請(qǐng)求
5.關(guān)閉HTTP偵聽(tīng)器
例如:我們要實(shí)現(xiàn)一個(gè)簡(jiǎn)單Http服務(wù),進(jìn)行文件的下載,或者進(jìn)行一些其他的操作,例如要發(fā)送郵件,使用HttpListener監(jiān)聽(tīng),處理郵件隊(duì)列,避免在網(wǎng)站上的同步等待。以及獲取一些緩存的數(shù)據(jù)等等行為
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Web; using System.IO; using Newtonsoft.Json; namespace HttpListenerApp { /// <summary> /// HttpRequest邏輯處理 /// </summary> public class HttpProvider { private static HttpListener httpFiledownload; //文件下載處理請(qǐng)求監(jiān)聽(tīng) private static HttpListener httOtherRequest; //其他超做請(qǐng)求監(jiān)聽(tīng) /// <summary> /// 開(kāi)啟HttpListener監(jiān)聽(tīng) /// </summary> public static void Init() { httpFiledownload = new HttpListener(); //創(chuàng)建監(jiān)聽(tīng)實(shí)例 httpFiledownload.Prefixes.Add("http://10.0.0.217:20009/FileManageApi/Download/"); //添加監(jiān)聽(tīng)地址 注意是以/結(jié)尾。 httpFiledownload.Start(); //允許該監(jiān)聽(tīng)地址接受請(qǐng)求的傳入。 Thread ThreadhttpFiledownload = new Thread(new ThreadStart(GethttpFiledownload)); //創(chuàng)建開(kāi)啟一個(gè)線程監(jiān)聽(tīng)該地址得請(qǐng)求 ThreadhttpFiledownload.Start(); httOtherRequest = new HttpListener(); httOtherRequest.Prefixes.Add("http://10.0.0.217:20009/BehaviorApi/EmailSend/"); //添加監(jiān)聽(tīng)地址 注意是以/結(jié)尾。 httOtherRequest.Start(); //允許該監(jiān)聽(tīng)地址接受請(qǐng)求的傳入。 Thread ThreadhttOtherRequest = new Thread(new ThreadStart(GethttOtherRequest)); ThreadhttOtherRequest.Start(); } /// <summary> /// 執(zhí)行文件下載處理請(qǐng)求監(jiān)聽(tīng)行為 /// </summary> public static void GethttpFiledownload() { while (true) { HttpListenerContext requestContext = httpFiledownload.GetContext(); //接受到新的請(qǐng)求 try { //reecontext 為開(kāi)啟線程傳入的 requestContext請(qǐng)求對(duì)象 Thread subthread = new Thread(new ParameterizedThreadStart((reecontext) => { Console.WriteLine("執(zhí)行文件處理請(qǐng)求監(jiān)聽(tīng)行為"); var request = (HttpListenerContext)reecontext; var image = HttpUtility.UrlDecode(request.Request.QueryString["imgname"]); //接受GET請(qǐng)求過(guò)來(lái)的參數(shù); string filepath = AppDomain.CurrentDomain.BaseDirectory + image; if (!File.Exists(filepath)) { filepath = AppDomain.CurrentDomain.BaseDirectory + "default.jpg"; //下載默認(rèn)圖片 } using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, (int)fs.Length); //將文件讀到緩存區(qū) request.Response.StatusCode = 200; request.Response.Headers.Add("Access-Control-Allow-Origin", "*"); request.Response.ContentType = "image/jpg"; request.Response.ContentLength74 = buffer.Length; var output = request.Response.OutputStream; //獲取請(qǐng)求流 output.Write(buffer, 0, buffer.Length); //將緩存區(qū)的字節(jié)數(shù)寫(xiě)入當(dāng)前請(qǐng)求流返回 output.Close(); } })); subthread.Start(requestContext); //開(kāi)啟處理線程處理下載文件 } catch (Exception ex) { try { requestContext.Response.StatusCode = 500; requestContext.Response.ContentType = "application/text"; requestContext.Response.ContentEncoding = Encoding.UTF8; byte[] buffer = System.Text.Encoding.UTF8.GetBytes("System Error"); //對(duì)客戶(hù)端輸出相應(yīng)信息. requestContext.Response.ContentLength74 = buffer.Length; System.IO.Stream output = requestContext.Response.OutputStream; output.Write(buffer, 0, buffer.Length); //關(guān)閉輸出流,釋放相應(yīng)資源 output.Close(); } catch { } } } } /// <summary> /// 執(zhí)行其他超做請(qǐng)求監(jiān)聽(tīng)行為 /// </summary> public static void GethttOtherRequest() { while (true) { HttpListenerContext requestContext = httOtherRequest.GetContext(); //接受到新的請(qǐng)求 try { //reecontext 為開(kāi)啟線程傳入的 requestContext請(qǐng)求對(duì)象 Thread subthread = new Thread(new ParameterizedThreadStart((reecontext) => { Console.WriteLine("執(zhí)行其他超做請(qǐng)求監(jiān)聽(tīng)行為"); var request = (HttpListenerContext)reecontext; var msg = HttpUtility.UrlDecode(request.Request.QueryString["behavior"]); //接受GET請(qǐng)求過(guò)來(lái)的參數(shù); //在此處執(zhí)行你需要進(jìn)行的操作>>比如什么緩存數(shù)據(jù)讀取,隊(duì)列消息處理,郵件消息隊(duì)列添加等等。 request.Response.StatusCode = 200; request.Response.Headers.Add("Access-Control-Allow-Origin", "*"); request.Response.ContentType = "application/json"; requestContext.Response.ContentEncoding = Encoding.UTF8; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new { success = true, behavior = msg })); request.Response.ContentLength74 = buffer.Length; var output = request.Response.OutputStream; output.Write(buffer, 0, buffer.Length); output.Close(); })); subthread.Start(requestContext); //開(kāi)啟處理線程處理下載文件 } catch (Exception ex) { try { requestContext.Response.StatusCode = 500; requestContext.Response.ContentType = "application/text"; requestContext.Response.ContentEncoding = Encoding.UTF8; byte[] buffer = System.Text.Encoding.UTF8.GetBytes("System Error"); //對(duì)客戶(hù)端輸出相應(yīng)信息. requestContext.Response.ContentLength74 = buffer.Length; System.IO.Stream output = requestContext.Response.OutputStream; output.Write(buffer, 0, buffer.Length); //關(guān)閉輸出流,釋放相應(yīng)資源 output.Close(); } catch { } } } } } }
調(diào)用方式:注意這里啟動(dòng)程序必須以管理員身份運(yùn)行,因?yàn)樯衔绲谋O(jiān)聽(tīng)需要開(kāi)啟端口,所有需要以管理員身份運(yùn)行。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HttpListenerApp { class Program { static void Main(string[] args) { //開(kāi)啟請(qǐng)求監(jiān)聽(tīng) HttpProvider.Init(); } } }
執(zhí)行后的結(jié)果為:
看完上述內(nèi)容,你們對(duì).NET中怎么利用HttpListener實(shí)現(xiàn)Http服務(wù)有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
本文標(biāo)題:.NET中怎么利用HttpListener實(shí)現(xiàn)Http服務(wù)-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)URL:http://jinyejixie.com/article30/djcspo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、全網(wǎng)營(yíng)銷(xiāo)推廣、品牌網(wǎng)站建設(shè)、面包屑導(dǎo)航、網(wǎng)站制作、軟件開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容