We help IT Professionals succeed at work.

using webrequest to get file list from website

flynny
flynny asked
on
658 Views
Last Modified: 2014-08-18
Hi all,

I have a dynamically generated site whereby I want to provide a list of fonts (.ttf files) that are present in a public folder on a common resource site i have setup.

for example i have user1.myurl.co.uk which will host the generated site. I then want to use AJAX to contact resources.myurl.co.uk/fonts (this will store all common resources for these generated sites).

now i have to following code;

WebRequest request = WebRequest.Create(fonts_path);
        WebResponse response = request.GetResponse();
        Regex regex = new Regex("<a href=\"?<url>.*\">(?<name>.*)</a>");
        List<KeyValuePair<String, String>> files = new List<KeyValuePair<String, String>>();

        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            string result = reader.ReadToEnd();

            MatchCollection matches = regex.Matches(result);
            if (matches.Count == 0)
            {
                Console.WriteLine("parse failed.");
                return null;
            }

            foreach (Match match in matches)
            {
                if (!match.Success) { continue; }

                files.Add(new KeyValuePair<String, String>(match.Groups["url"].ToString(),match.Groups["name"].ToString()));
                Console.WriteLine(match.Groups["name"]);
            }
        }

        return files;

Open in new window


now the url is giving a 403 forbidden error on the webrequest. I have tried adding the .htaccess file to the root directory.

Can anyone please tell me what the issue coudl be herE?
Comment
Watch Question

IT Manager
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
The only way to get a list of files available in a directory on a web server would be to parse the HTML file that is generated for directories where directory browsing is enabled. Directory browsing is typically not enabled as a security precaution. I would suggest writing a small web-service-like endpoint that will browse the directory (on the server) and return a list of file names.

Author

Commented:
Hi all,

sorry yes I am getting the 403 in the browser as well.

the server is running IIS 7. so yes, it appears its probably more web server issues than programming.

Author

Commented:
sorry guys solved the issue.

IIS 7 allows me to switch on directory browser in the direcotry browsing snap in.

thanks for the help.

Author

Commented:
not a solution but helped point me in the right direction.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Hmmmm... I thought I was the one who mentioned directory browsing. Alas...

Author

Commented:
sorry kaufmed i didnt see your post can i give some point to you somehow?
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.