C# - List all the links in a website.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Text.RegularExpressions;

namespace ProgrammingDotNet
{
    class Program
    {
        public static void Main(string[] args)
        {
            StreamReader reader = null;
            try
            {
                WebRequest request = WebRequest.Create("http://www.techexams.net/");
                WebResponse response = request.GetResponse();
                reader = new StreamReader(response.GetResponseStream());
                string content = reader.ReadToEnd();

                Regex regex = new Regex("href\\s*=\\s*\"([^\"]*)\"", RegexOptions.IgnoreCase);

                MatchCollection matches = regex.Matches(content);
                foreach (Match match in matches)
                {
                    Console.WriteLine(match.Groups[1]);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            Console.ReadLine();
        }
    }
}

Comments

Popular posts from this blog

Offers on Friday, April 24, 2020

Fatal: LoadModule: error loading module 'mod_sql_mysql.c'