C#中的HttpWebRequest命令来获取目录列表 [英] C# HttpWebRequest command to get directory listing

查看:354
本文介绍了C#中的HttpWebRequest命令来获取目录列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个简短code片段来得到一个目录从HTTP服务器列表。

I need a short code snippet to get a directory listing from an HTTP server.

感谢

推荐答案

在code前几个重要的注意事项:

A few important considerations before the code:


  1. HTTP服务器已被配置为允许目录,列出你想要的目录;

  2. 由于目录列表是正常的HTML网页,没有标准,它定义目录列表格式;

  3. 由于考虑 2 你是在你必须把具体的code每个服务器的土地。

  1. The HTTP Server has to be configured to allow directories listing for the directories you want;
  2. Because directory listings are normal HTML pages there is no standard that defines the format of a directory listing;
  3. Due to consideration 2 you are in the land where you have to put specific code for each server.

我的选择是使用常规的前pressions。这允许快速解析和定制。你可以得到每个站点的具体定期EX pressions模式,这样你有一个非常模块化方法。如果您打算提高解析模块与新网站在不改变源$ C ​​$ C支持使用URL映射到正规的前pression模式的外部来源。

My choice is to use regular expressions. This allows for rapid parsing and customization. You can get specific regular expressions pattern per site and that way you have a very modular approach. Use an external source for mapping URL to regular expression patterns if you plan to enhance the parsing module with new sites support without changing the source code.

为例,从 http://www.ibiblio.org/pub/

namespace Example
{
    using System;
    using System.Net;
    using System.IO;
    using System.Text.RegularExpressions;

    public class MyExample
    {
        public static string GetDirectoryListingRegexForUrl(string url)
        {
            if (url.Equals("http://www.ibiblio.org/pub/"))
            {
                return "<a href=\".*\">(?<name>.*)</a>";
            }
            throw new NotSupportedException();
        }
        public static void Main(String[] args)
        {
            string url = "http://www.ibiblio.org/pub/";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string html = reader.ReadToEnd();
                    Regex regex = new Regex(GetDirectoryListingRegexForUrl(url));
                    MatchCollection matches = regex.Matches(html);
                    if (matches.Count > 0)
                    {
                        foreach (Match match in matches)
                        {
                            if (match.Success)
                            {
                                Console.WriteLine(match.Groups["name"]);
                            }
                        }
                    }
                }
            }

            Console.ReadLine();
        }
    }
}

这篇关于C#中的HttpWebRequest命令来获取目录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆