• 大小: 3KB
    文件类型: .7z
    金币: 2
    下载: 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

评论

共有 条评论