C#获取HTTP路径下的所有文件信息(可以做侦测了)

早上看到BBS上有这个贴子,"知道文件夹的路径 怎么把那个文件夹里的全部图片copy下来"

看到这样一个命题,首先想到是如何获取该路径下的全部的目录结构.我们有IE里有时是可以看到某个站点的目录结构的.如FTP不就是这样一个例子吗.首先该站点要支持目录浏览的功能.如果已关闭这个功能,那我暂时也想不出办法.

HTTP里我们常用get,post或head方法来获取站点的文件或相关信息.而这些对于上述要求是做不到的.这里使用了"PROPFIND"方法来实现该需求.有兴趣的话可以了解一下相关的资料.

话不多说了.直接上代码.

 

      public class Resource<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

        {

            public string Name;

            public bool IsFolder;

            public string Url;

            public DateTime  LastModified;

        }

        public static SortedList GetDirectoryContents(string url, bool deep)

        {

            //Retrieve the File

            HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(url);

            Request.Headers.Add("Translate: f");

            Request.Credentials = CredentialCache.DefaultCredentials;

 

            string requestString = "<?xml version=/"1.0/" encoding=/"utf-8/" ?>" +

                "<a:propfind xmlns:a=/"DAV:/">" +

                "<a:prop>" +

                "<a:displayname/>" +

                "<a:iscollection/>" +

                "<a:getlastmodified/>" +

                "</a:prop>" +

                "</a:propfind>";

 

            Request.Method = "PROPFIND";

            if (deep == true)

                Request.Headers.Add("Depth: infinity");

            else

                Request.Headers.Add("Depth: 1");

            Request.ContentLength = requestString.Length;

            Request.ContentType = "text/xml";

 

            Stream requestStream = Request.GetRequestStream();

            requestStream.Write(Encoding.ASCII.GetBytes(requestString), 0, Encoding.ASCII.GetBytes(requestString).Length);

            requestStream.Close();

 

            HttpWebResponse Response;

            StreamReader respStream;

            try

            {

                Response = (HttpWebResponse)Request.GetResponse();

                respStream = new StreamReader(Response.GetResponseStream());

            }

            catch (WebException e)

            {

                Debug.WriteLine("错误" + url);

                throw e;

            }

 

            StringBuilder SB = new StringBuilder();

 

            char[] respChar = new char[1024];

            int BytesRead = 0;

 

            BytesRead = respStream.Read(respChar, 0, 1024);

 

            while (BytesRead > 0)

            {

                SB.Append(respChar, 0, BytesRead);

                BytesRead = respStream.Read(respChar, 0, 1024);

            }

            respStream.Close();

 

            XmlDocument XmlDoc = new XmlDocument();

            XmlDoc.LoadXml(SB.ToString());

 

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(XmlDoc.NameTable);

            nsmgr.AddNamespace("a", "DAV:");

 

            XmlNodeList NameList = XmlDoc.SelectNodes("//a:prop/a:displayname", nsmgr);

            XmlNodeList isFolderList = XmlDoc.SelectNodes("//a:prop/a:iscollection", nsmgr);

            XmlNodeList LastModList = XmlDoc.SelectNodes("//a:prop/a:getlastmodified", nsmgr);

            XmlNodeList HrefList = XmlDoc.SelectNodes("//a:href", nsmgr);

 

            SortedList ResourceList = new SortedList();

           Resource tempResource;

 

            for (int i = 0; i < NameList.Count; i++)

            {

                if (HrefList[i].InnerText.ToLower(new CultureInfo("en-US")).TrimEnd(new char[] { '/' }) != url.ToLower(new CultureInfo("en-US")).TrimEnd(new char[] { '/' }))

                {

                    tempResource = new Resource();

                    tempResource.Name = NameList[i].InnerText;

                    tempResource.IsFolder = Convert.ToBoolean(Convert.ToInt32(isFolderList[i].InnerText));

                    tempResource.Url = HrefList[i].InnerText;

                    tempResource.LastModified = Convert.ToDateTime(LastModList[i].InnerText);

                    ResourceList.Add(tempResource.Url, tempResource);

                }

            }

 

            return ResourceList;

        }

 

主要利用了WebDAV 的功能来实现.请把站点的目录浏览权限给打开,使用时调用上面的方法即可获取相关的信息.如果想下载的话,那可以根据获取的URL再次进行下载.

 

展开阅读全文

没有更多推荐了,返回首页