C#Web服务器请求的目录问题

C#Web服务器请求的目录问题,c#,webserver,C#,Webserver,我正在尝试用C#制作一个web服务器,我需要获取请求的url,然后列出请求的文件和文件夹。这对于获取第一个目录很好 例如,我的Web服务器根是c:\test当我打开localhost时,我会得到test文件夹的内容。假设数据是c:\test的子文件夹,我现在可以从浏览器中单击数据并进入c:\test\Data。当我单击任何文件夹时,get请求将带有%2F而不是c:\test\Data\ok,因此我被卡住了 接收请求的代码: sRequest = sBuffer.Substring(0, iSt

我正在尝试用C#制作一个web服务器,我需要获取请求的url,然后列出请求的文件和文件夹。这对于获取第一个目录很好

例如,我的Web服务器根是c:\test当我打开localhost时,我会得到test文件夹的内容。假设数据是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);
                    }
sRequest=sBuffer.Substring(0,iStartPos-1);
sRequest.Replace(“\\”,“/”);
如果((sRequest.IndexOf(“.”<1)和(!sRequest.EndsWith(“/”))
{
sRequest=sRequest+“/”;
}
iStartPos=sRequest.LastIndexOf(“/”)+1;
sRequestedFile=sRequest.Substring(iStartPos);
sDirName=sRequest.Substring(sRequest.IndexOf(“/”),sRequest.LastIndexOf(“/”)-3);
如果(sDirName==“/”)
sLocalDir=sMyWebServerRoot;
其他的
{
//获取虚拟目录
//sLocalDir=GetLocalPath(sMyWebServerRoot,sDirName);
Console.WriteLine(“我在这里”);
sDirName=sDirName.Substring(1,sDirName.Length-2);
//sDirName=sDirName.Replace(“/”,“\\”);
Console.WriteLine(“Amit:+sDirName”);
字符串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);
        }
publicstringlistdir(stringslocaldir,stringsdirname)
{
字符串sresult=“”;
StringBuilder sb=新的StringBuilder();
某人加上一行(“”);
某人加上一行(“”);
(b)附加(“测试”);
某人加上一行(“”);
某人加上一行(“”);
sb.AppendLine(“在“+sLocaldir+”下列出文件夹”);
字符串[]folderpath=Directory.GetDirectories(sLocaldir);
sb.AppendLine(“列出目录
”); for(int i=0;i”), sb.AppendLine(字符串格式(“
”), HttpUtility.HtmlEncode(HttpUtility.UrlEncode(全名)), HttpUtility.HtmlEncode(fname)); } string[]filepath=Directory.GetFiles(@“C:\test”); sb.AppendLine(“列出文件
”); for(int i=0;i”), HttpUtility.HtmlEncode(HttpUtility.UrlEncode(名称)), HttpUtility.HtmlEncode(名称)); } sb.附录(“”); 某人加上一行(“”); 某人加上一行(“”); sresult=sb.ToString(); 返回sresult; //控制台写入线(sresult); }
任何帮助都将不胜感激


谢谢你

%2F是/符号的安全编码。你在上面的代码中对/符号进行了HTML编码

您的方法可以简单得多,请参见:


好的,伙计们,我得到了我自己问题的答案,但出现了另一个问题,即使用下面给定的代码,我在windows应用程序窗体中出现错误,但相同的代码在控制台应用程序中可以正常工作。Byte[]bReceive=新字节[1024];int i=mySocket.Receive(bReceive,bReceive.Length,0);//将字节转换为字符串sBuffer=Encoding.ASCII.GetString(bReceive);