我写了一些代码,可以获取IIS http站点下的所有路径信息,如果它允许目录列表,最后你可以这样做:
List<PathInfo> pathInfos = new List<PathInfo>();
HttpHelper.GetAllFilePathAndSubDirectory("http://localhost:33333/", pathInfos);
HttpHelper.PrintAllPathInfo(pathInfos);
辅助代码,正则表达式可以自己定制,或者改用html解析器):
public static class HttpHelper
{
public static string ReadHtmlContentFromUrl(string url)
{
string html = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
//Console.WriteLine(html);
return html;
}
public static void GetAllFilePathAndSubDirectory(string baseUrl, List<PathInfo> pathInfos)
{
Uri baseUri = new Uri( baseUrl.TrimEnd('/') );
string rootUrl = baseUri.GetLeftPart(UriPartial.Authority);
Regex regexFile = new Regex("[0-9] <a href=\"(http:)?(?<file>.*?)\"", RegexOptions.IgnoreCase);
Regex regexDir = new Regex("dir.*?<a href=\"(http:)?(?<dir>.*?)\"", RegexOptions.IgnoreCase);
string html = ReadHtmlContentFromUrl(baseUrl);
//Files
MatchCollection matchesFile = regexFile.Matches(html);
if (matchesFile.Count != 0)
foreach (Match match in matchesFile)
if (match.Success)
pathInfos.Add(
new PathInfo( rootUrl + match.Groups["file"], false));
//Dir
MatchCollection matchesDir = regexDir.Matches(html);
if (matchesDir.Count != 0)
foreach (Match match in matchesDir)
if (match.Success)
{
var dirInfo = new PathInfo(rootUrl + match.Groups["dir"], true);
GetAllFilePathAndSubDirectory(dirInfo.AbsoluteUrlStr, dirInfo.Childs);
pathInfos.Add(dirInfo);
}
}
public static void PrintAllPathInfo(List<PathInfo> pathInfos)
{
pathInfos.ForEach(f =>
{
Console.WriteLine(f.AbsoluteUrlStr);
PrintAllPathInfo(f.Childs);
});
}
}
public class PathInfo
{
public PathInfo(string absoluteUri, bool isDir)
{
AbsoluteUrl = new Uri(absoluteUri);
IsDir = isDir;
Childs = new List<PathInfo>();
}
public Uri AbsoluteUrl { get; set; }
public string AbsoluteUrlStr
{
get { return AbsoluteUrl.ToString(); }
}
public string RootUrl
{
get { return AbsoluteUrl.GetLeftPart(UriPartial.Authority); }
}
public string RelativeUrl
{
get { return AbsoluteUrl.PathAndQuery; }
}
public string Query
{
get { return AbsoluteUrl.Query; }
}
public bool IsDir { get; set; }
public List<PathInfo> Childs { get; set; }
public override string ToString()
{
return String.Format("{0} IsDir {1} ChildCount {2} AbsUrl {3}", RelativeUrl, IsDir, Childs.Count, AbsoluteUrlStr);
}
}