using webrequest to get file list from website

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 which will host the generated site. I then want to use AJAX to contact (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()));

        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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Are you sure this is a programming issue you need help with (see your topics you entered), or is this a web server thing you need help with?
If you are sure about the url, use the url in the browser. It's NOT possible you use exactly the same URL and it's visible in the browser and gives a 403 error in the application (other than a server side check on cookies, headers and what not).

Use Wireshark to analyse some of the traffic (if you're sure URLs are correct), try to match your code as close as possible (headers, useragent, cookies etc)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽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.
flynnyAuthor 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.
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

flynnyAuthor 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.
flynnyAuthor Commented:
not a solution but helped point me in the right direction.
käµfm³d 👽Commented:
Hmmmm... I thought I was the one who mentioned directory browsing. Alas...
flynnyAuthor Commented:
sorry kaufmed i didnt see your post can i give some point to you somehow?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.