ホームページ >バックエンド開発 >C#.Net チュートリアル >.NET での HttpListener を介した単純な Http サービスの実装
HttpListener は、プログラムで制御可能なシンプルな HTTP プロトコル リスナーを提供します。これを使用すると、IIS などの大規模なサービス プログラムを起動せずに、一部の HTTP サービスを簡単に提供できます。 HttpListener を使用するプロセスは非常に簡単です。主に次の手順に分かれています
1. HTTP リスナー オブジェクトを作成し、初期化します
2. リッスンする必要がある URI プレフィックスを追加します
3.クライアントからのリクエスト
4. クライアントの HTTP リクエストを処理します
5. HTTP リスナーを閉じる
例: 単純な Http サービスを実装し、ファイルをダウンロードするか、メールの送信などのその他の操作を実行したいとします。 HttpListener は、メール キューをリッスンして処理し、Web サイトでの同期待機を回避します。そして、キャッシュされたデータとその他の動作を取得します
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; //文件下载处理请求监听 private static HttpListener httOtherRequest; //其他超做请求监听 /// <summary> /// 开启HttpListener监听 /// </summary> public static void Init() { httpFiledownload = new HttpListener(); //创建监听实例 httpFiledownload.Prefixes.Add("http://10.0.0.217:20009/FileManageApi/Download/"); //添加监听地址 注意是以/结尾。 httpFiledownload.Start(); //允许该监听地址接受请求的传入。 Thread ThreadhttpFiledownload = new Thread(new ThreadStart(GethttpFiledownload)); //创建开启一个线程监听该地址得请求 ThreadhttpFiledownload.Start(); httOtherRequest = new HttpListener(); httOtherRequest.Prefixes.Add("http://10.0.0.217:20009/BehaviorApi/EmailSend/"); //添加监听地址 注意是以/结尾。 httOtherRequest.Start(); //允许该监听地址接受请求的传入。 Thread ThreadhttOtherRequest = new Thread(new ThreadStart(GethttOtherRequest)); ThreadhttOtherRequest.Start(); } /// <summary> /// 执行文件下载处理请求监听行为 /// </summary> public static void GethttpFiledownload() { while (true) { HttpListenerContext requestContext = httpFiledownload.GetContext(); //接受到新的请求 try { //reecontext 为开启线程传入的 requestContext请求对象 Thread subthread = new Thread(new ParameterizedThreadStart((reecontext) => { Console.WriteLine("执行文件处理请求监听行为"); var request = (HttpListenerContext)reecontext; var image = HttpUtility.UrlDecode(request.Request.QueryString["imgname"]); //接受GET请求过来的参数; string filepath = AppDomain.CurrentDomain.BaseDirectory + image; if (!File.Exists(filepath)) { filepath = AppDomain.CurrentDomain.BaseDirectory + "default.jpg"; //下载默认图片 } using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, (int)fs.Length); //将文件读到缓存区 request.Response.StatusCode = 200; request.Response.Headers.Add("Access-Control-Allow-Origin", "*"); request.Response.ContentType = "image/jpg"; request.Response.ContentLength64 = buffer.Length; var output = request.Response.OutputStream; //获取请求流 output.Write(buffer, 0, buffer.Length); //将缓存区的字节数写入当前请求流返回 output.Close(); } })); subthread.Start(requestContext); //开启处理线程处理下载文件 } 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"); //对客户端输出相应信息. requestContext.Response.ContentLength64 = buffer.Length; System.IO.Stream output = requestContext.Response.OutputStream; output.Write(buffer, 0, buffer.Length); //关闭输出流,释放相应资源 output.Close(); } catch { } } } } /// <summary> /// 执行其他超做请求监听行为 /// </summary> public static void GethttOtherRequest() { while (true) { HttpListenerContext requestContext = httOtherRequest.GetContext(); //接受到新的请求 try { //reecontext 为开启线程传入的 requestContext请求对象 Thread subthread = new Thread(new ParameterizedThreadStart((reecontext) => { Console.WriteLine("执行其他超做请求监听行为"); var request = (HttpListenerContext)reecontext; var msg = HttpUtility.UrlDecode(request.Request.QueryString["behavior"]); //接受GET请求过来的参数; //在此处执行你需要进行的操作>>比如什么缓存数据读取,队列消息处理,邮件消息队列添加等等。 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.ContentLength64 = buffer.Length; var output = request.Response.OutputStream; output.Write(buffer, 0, buffer.Length); output.Close(); })); subthread.Start(requestContext); //开启处理线程处理下载文件 } 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"); //对客户端输出相应信息. requestContext.Response.ContentLength64 = buffer.Length; System.IO.Stream output = requestContext.Response.OutputStream; output.Write(buffer, 0, buffer.Length); //关闭输出流,释放相应资源 output.Close(); } catch { } } } } } }
メソッドの呼び出し: ここでの起動プログラムは管理者として実行する必要があることに注意してください。朝の監視ではポートを開く必要があるため、すべてを管理者として実行する必要があります。 。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HttpListenerApp { class Program { static void Main(string[] args) { //开启请求监听 HttpProvider.Init(); } } }
実行後の結果は次のとおりです:
ここでは、HttpListener を使用して単純な制御プログラムを介して単純な Http サービス プログラムが実装されています。スレッド数が少なく、非同期処理が可能です。たとえば、行動情報の要求を受信したときに、それを最初にユーザーに返すことができるため、ユーザーは同期的に待たずに次の操作を実行できます。リクエストを HttpListener に送信して受信する単純なメール サーバー。リクエストの直後に戻り、メールを送信するためにキューに残します。メールの送信が遅れる場合がございますので、お待ちいただく必要はございません。
上記は、.NET での HttpListener を介した単純な Http サービスの実装の内容です。その他の関連コンテンツについては、PHP 中国語 Web サイト (www.php.cn) に注目してください。