Esempio n. 1
0
    /// <summary>
    /// 启动服务
    /// </summary>
    public void init(string host, string port, string webHomeDir)
    {
        mHost       = host;
        mPort       = port;
        mWebHomeDir = webHomeDir;
        if (mHost == "")
        {
            return;
        }
        mListener    = new HttpListener();
        mHttpFactory = new HttpPacketFactory();
        mHttpFactory.init();
        if (mListener.IsListening)
        {
            return;
        }

        mListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;        //指定身份验证 Anonymous匿名访问
        if (!string.IsNullOrEmpty(mHost) && mHost.Length > 0)
        {
            mListener.Prefixes.Add("http://" + mHost + ":" + mPort + "/");
        }
        else if (mListener.Prefixes == null || mListener.Prefixes.Count == 0)
        {
            mListener.Prefixes.Add("http://localhost:" + mPort + "/");
        }

        mListener.Start();
        mListenThread      = new Thread(AcceptClient);
        mListenThread.Name = "httpserver";
        mListenThread.Start();
    }
Esempio n. 2
0
    //处理客户端请求
    private void HandleRequest(object ctx)
    {
        HttpListenerContext  context  = ctx as HttpListenerContext;
        HttpListenerResponse response = context.Response;
        HttpListenerRequest  request  = context.Request;

        try
        {
            string rawUrl          = request.RawUrl;
            int    paramStartIndex = rawUrl.IndexOf('?');
            if (paramStartIndex > 0)
            {
                rawUrl = rawUrl.Substring(0, paramStartIndex);
            }
            else if (paramStartIndex == 0)
            {
                rawUrl = "";
            }


            #region 网页请求
            //string InputStream = "";
            using (var streamReader = new StreamReader(request.InputStream))
            {
                //InputStream = streamReader.ReadToEnd();
            }
            string filePath = "";
            if (string.IsNullOrEmpty(rawUrl) || rawUrl.Length == 0 || rawUrl == "/")
            {
                filePath = WebHomeDir + "/index.html";                // + directorySeparatorChar + "Index.html";
            }
            else
            {
                filePath = WebHomeDir + rawUrl;                //.Replace("/", directorySeparatorChar);
            }
            if (!File.Exists(filePath))
            {
                HttpPacket packet = HttpPacketFactory.createPacket(rawUrl);
                if (null != packet)
                {
                    packet.handlePacket(ref response, ref request);
                }
                else
                {
                    response.ContentLength64 = 0;
                    response.StatusCode      = 404;
                    response.Abort();
                }
            }
            else
            {
                response.StatusCode = 200;
                string exeName = Path.GetExtension(filePath);
                response.ContentType = GetContentType(exeName);
                FileStream fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite);
                int        byteLength = (int)fileStream.Length;
                byte[]     fileBytes  = new byte[byteLength];
                fileStream.Read(fileBytes, 0, byteLength);
                fileStream.Flush();
                fileStream.Dispose();
                response.ContentLength64 = byteLength;
                response.OutputStream.Write(fileBytes, 0, byteLength);
                response.OutputStream.Flush();
            }
            #endregion
        }
        catch (Exception ex)
        {
            UnityUtility.logInfo(typeof(HttpServerManager) + ex.Message);
            response.StatusCode  = 200;
            response.ContentType = "text/plain";
            using (StreamWriter writer = new StreamWriter(response.OutputStream, Encoding.UTF8))
            {
                writer.WriteLine("接收完成!");
            }
        }
        try
        {
            response.Close();
        }
        catch (Exception ex)
        {
            UnityUtility.logInfo(typeof(HttpServerManager) + ex.Message);
        }
    }