C#Webserver请求目录问题

时间:2010-03-03 17:59:14

标签: c# webserver

我正在尝试使用C#创建一个Web服务器,我需要获取所请求的URL,然后列出所请求的文件和文件夹。这样可以获得第一个目录。

对于Eg。我的网络服务器根目录是c:\ test当我打开localhost时,我得到了test文件夹的内容。 Say Data是c:\ test的子文件夹,我可以点击浏览器中的数据并进入C:\ test \ data现在当我点击任何文件夹然后get请求附带%2F而不是c:\ test \ data \ ok,所以我被卡住了。

接收请求的代码:

 sRequest = sBuffer.Substring(0, iStartPos - 1);
    sRequest.Replace("\\", "/");
 if ((sRequest.IndexOf(".") < 1) && (!sRequest.EndsWith("/")))
                    {
                        sRequest = sRequest + "/";

                    }
iStartPos = sRequest.LastIndexOf("/") + 1;
                    sRequestedFile = sRequest.Substring(iStartPos);
sDirName = sRequest.Substring(sRequest.IndexOf("/"), sRequest.LastIndexOf("/") - 3);

if (sDirName == "/")
                        sLocalDir = sMyWebServerRoot;
                    else
                    {
                        //Get the Virtual Directory
                        // sLocalDir = GetLocalPath(sMyWebServerRoot, sDirName);
                        Console.WriteLine("i am here");
                        sDirName = sDirName.Substring(1, sDirName.Length - 2);
                        //sDirName = sDirName.Replace("/", "\\");
                        Console.WriteLine("Amit:" + sDirName);
                        string test1 = Path.Combine("C:\\test\\", sDirName);
                        sLocalDir = Path.Combine(@"C:\\test", sDirName);
                    }

现在列表目录我有以下功能:

public String listdir(string sLocaldir,string sDirName)
        {
            string sresult = "";
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<html>");
            sb.AppendLine("<head>");
            sb.AppendLine("<title>Test</title>");
            sb.AppendLine("</head>");
            sb.AppendLine("<body>");
            sb.AppendLine("<h1><center><u>Listing Folders Under " + sLocaldir + "</h1></u></center>");
            string[] folderpaths = Directory.GetDirectories(sLocaldir);

            sb.AppendLine("<font color=red><font size=5>Listing Directories<br>");
            for (int i = 0; i < folderpaths.Length; i++)
            {
                string fpath = folderpaths[i];
                string[] foldernames = fpath.Split('\\');
                int j = foldernames.Length - 1;
                string fname = foldernames[j];
                string fullname;
                if (sDirName != "/")
                {
                  //fname= fname + "\\";
                  fullname = sDirName +"/"+ fname;
                  //fullname = fullname.Replace("\\", "/");
                    //fullname = Path.GetPathRoot("C:\\test");
                    Console.WriteLine("Get full path:" + fullname);
                }
                else
                {
                    fullname = fname;
                }
                Console.WriteLine("Full Test:" + fullname);
                //sb.AppendLine(string.Format("<img src=file.png height=20 width=20><a href=\"{0}\">{1}</a><br>",
                sb.AppendLine(string.Format("<img src=file.png height=20 width=20><a href=\"{0}\">{1}</a><br>",
                    HttpUtility.HtmlEncode(HttpUtility.UrlEncode(fullname )),
                    HttpUtility.HtmlEncode(fname)));
            }
            string[] filePaths = Directory.GetFiles(@"C:\test");
            sb.AppendLine("<font color=red><font size=5>Listing Files<br>");
            for (int i = 0; i < filePaths.Length; ++i)
            {
                string name = Path.GetFileName(filePaths[i]);

                sb.AppendLine(string.Format("<img src=file.png height=20 width=20><a href=\"{0}\">{1}</a><br>",
                    HttpUtility.HtmlEncode(HttpUtility.UrlEncode(name)),
                    HttpUtility.HtmlEncode(name)));
            }

            sb.AppendLine("</ul>");
            sb.AppendLine("</body>");
            sb.AppendLine("</html>");
            sresult = sb.ToString();
            return sresult;
            //Console.WriteLine(sresult);
        }

任何帮助都将受到高度赞赏。

谢谢

1 个答案:

答案 0 :(得分:0)

%2F是/符号的安全编码。您正在HTMLE编码上面代码中的/符号。

您的方法可以更简单地看到:

http://www.codeproject.com/KB/IP/mywebserver.aspx