91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 3KB
    文件類型: .7z
    金幣: 1
    下載: 1 次
    發布日期: 2021-05-14
  • 語言: C#
  • 標簽: httplistener??高并發??C#??

資源簡介

C#調用httplistener實現簡單的http服務器例子:編譯后是一個控制臺應用程序,啟動后,可通過 http://127.0.0.1/ 訪問,采用了回調模式提供http服務,支持高并發

資源截圖

代碼片段和文件信息


using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Net;
using?System.Net.Sockets;
using?System.IO;
using?System.Web;

namespace?ConsoleApplication1
{
????class?Program
????{
????????static?HttpListener?sSocket?=?null;
????????public?static?string?modulePath;

????????static?void?Main(string[]?args)
????????{
????????????sSocket?=?new?HttpListener();
????????????sSocket.Prefixes.Add(“http://127.0.0.1:80/“);
????????????sSocket.Start();
????????????sSocket.BeginGetContext(new?AsyncCallback(GetContextCallBack)?sSocket);

????????????modulePath?=?System.AppDomain.CurrentDomain.baseDirectory;
????????????Console.WriteLine(“modulePath?:?“+modulePath+“\n“);
????????????Console.Read();
????????}

????????static?void?GetContextCallBack(IAsyncResult?ar)
????????{
????????????try
????????????{
????????????????sSocket?=?ar.AsyncState?as?HttpListener;
????????????????HttpListenerContext?context?=?sSocket.EndGetContext(ar);
????????????????sSocket.BeginGetContext(new?AsyncCallback(GetContextCallBack)?sSocket);
????????????????HttpListenerRequest?request?=?context.Request;
????????????????HttpListenerResponse?response?=?context.Response;
????????????????Console.WriteLine(request.Url.PathAndQuery);

????????????????string?username?=?HttpUtility.ParseQueryString(request.Url.Query).Get(“username“);
????????????????string?pwd?=?HttpUtility.ParseQueryString(request.Url.Query).Get(“pwd“);

????????????????string?absPath?=?request.Url.AbsolutePath.Substring(1);
????????????????Console.WriteLine(“absPath:“+absPath);

????????????????if?(absPath?==?“favicon.ico“)
????????????????{
????????????????????string?filename=Path.Combine(modulePathabsPath);
????????????????????if(File.Exists(filename))
????????????????????{
????????????????????????try
????????????????????????{
????????????????????????????Console.WriteLine(“Process?ico“);
????????????????????????????Stream?input?=?new?FileStream(filename?FileMode.Open);
????????????????????????????string?mime?=?“image/x-icon“;
????????????????????????????response.ContentType?=?mime;
????????????????????????????response.ContentLength64?=?input.Length;
????????????????????????????response.AddHeader(“Date“?DateTime.Now.ToString(“r“));
????????????????????????????response.AddHeader(“Last-Modified“?File.GetLastWriteTime(filename).ToString(“r“));

????????????????????????????byte[]?obuffer?=?new?byte[1024?*?32];
????????????????????????????int?nbytes;
????????????????????????????while?((nbytes?=?input.Read(obuffer?0?obuffer.Length))?>?0)
????????????????????????????{
????????????????????????????????response.OutputStream.Write(obuffer?0?nbytes);
????????????????????????????}
????????????????????????????
????????????????????????????input.Close();
????????????????????????????response.OutputStream.Flush();
????????????????????????????response.StatusCode?=?(int)HttpStatusCode.OK;
????????????????????????????obuff

評論

共有 條評論