Пример #1
0
        private static string FillDirectoryContext(string strFilePath, HttpListenerRequest request)
        {
            string strRet = "Context of directory: \"" + strFilePath + "\"<p>";

            try
            {
                string[] dirs = Directory.GetDirectories(strFilePath);
                foreach (string strDirPath in dirs)
                {
                    // dirs keep full path. We need only relative path in directory.
                    // So we need to split by '\' and take the last directory,
                    string[] splitDir = strDirPath.Split(fwdSlashDelim);
                    string   strDir   = splitDir[splitDir.Length - 1];
                    string   strUrl   = request.RawUrl;
                    if (strUrl[strUrl.Length - 1] != '/')
                    {
                        strUrl += '/';
                    }
                    strRet += "Dir  - " +
                              "<A HREF=\"" + strUrl + strDir + "\">" + strDir + "</A><br>";
                }

                string[] files = Directory.GetFiles(strFilePath);
                foreach (string locFilePath in files)
                {
                    string[] splitFile = locFilePath.Split(fwdSlashDelim);
                    string   strFile   = splitFile[splitFile.Length - 1];
                    string   strUrl    = request.RawUrl;
                    if (strUrl[strUrl.Length - 1] != '/')
                    {
                        strUrl += '/';
                    }

                    strRet += "File - " +
                              "<A HREF=\"" + strUrl + strFile + "\">" + strFile + "</A><br>";
                }
            }
            catch
            {
            }

            // Following line adds file post capabilities
            strRet += "<p>Below is example of sending file data to HTTP server";
            strRet += Resource1.GetString(Resource1.StringResources.PostForm);
            // Next line shows link protected by password.
            strRet += "<br>Below is example of protected link.";
            strRet += "<br><A HREF=\"/PasswordProtected\">Password Protected Secure Area</A>";
            strRet += "<br>Use following credentials to access it:<br> User Name: Igor<br> Password: MyPassword<br>";

            return(strRet);
        }
Пример #2
0
        public void Start(string prefix, int port)
        {
            if (listener != null)
            {
                return;
            }

            listener = new HttpListener(prefix, port);

            // Loads certificate if it is https server.
            if (prefix == "https")
            {
                // SKU == 3 indicates the device is the emulator.
                // The emulator and the device use different certificate types.  The emulator requires the use of a .PFX certficate, whereas the device simply
                // requires a CER certificate with appended private key. In addtion, the .PFX certificate requires a password ("NetMF").
                if (Utils.IsEmulator)
                {
                    listener.HttpsCert = new X509Certificate(m_emulatorCertData, "NetMF");
                }
                else
                {
                    string serverCertAsString = Resource1.GetString(Resource1.StringResources.cert_device_microsoft_com);
                    listener.HttpsCert = new X509Certificate(Encoding.UTF8.GetBytes(serverCertAsString));
                }
            }

            listener.Start();
            isStopped = false;

            new Thread(() =>
            {
                while (!isStopped)
                {
                    try
                    {
                        if (!listener.IsListening)
                        {
                            listener.Start();
                        }

                        HttpListenerContext context = listener.GetContext();

                        // see http://netmf.codeplex.com/workitem/2157
                        //new Thread(() =>
                        {
                            try
                            {
                                if (OnRequest != null)
                                {
                                    OnRequest(context.Request);
                                }

                                switch (context.Request.HttpMethod.ToUpper())
                                {
                                case "GET": ProcessClientGetRequest(context); break;

                                case "POST": ProcessClientPostRequest(context); break;
                                }

                                // for test:
                                //SendStream(Encoding.UTF8.GetBytes("<html><body>" + DateTime.Now + "</body></html>"), "text/html", context.Response);

                                context.Close();
                            }
                            catch (Exception e)
                            {
                                Debug.Print(e.Message);
                            }
                        }
                        //).Start();
                    }
                    //catch (InvalidOperationException ex)
                    //{
                    //    listener.Stop();
                    //    Thread.Sleep(1000);
                    //}
                    //catch (ObjectDisposedException ex)
                    //{
                    //    listener.Start();
                    //}
                    //catch (SocketException ex)
                    //{
                    //    if (ex.ErrorCode == 10053)
                    //        listener.Stop();
                    //}
                    catch (Exception ex)
                    {
                        //Thread.Sleep(1000);
                        //if (context != null)
                        //    context.Close();
                    }
                }

                // stopped
                listener.Close();
                listener = null;
            })
            {
                Priority = ThreadPriority.Normal
            }.Start();
        }