Download HTM files : WebClient « Network « C# / CSharp Tutorial






using System;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;

class MainClass
{
    private static void Main() 
    {
        string remoteUri = "http://www.java2s.com";
        
        WebClient client = new WebClient();

        string str = client.DownloadString(remoteUri);

        MatchCollection matches = Regex.Matches(str,@"http\S+[^-,;:?]\.htm");
        
        foreach(Match match in matches) 
        {
            foreach(Group grp in match.Groups) 
            {
                string file = grp.Value.Substring(grp.Value.LastIndexOf('/')+1);
                try
                {
                    Console.WriteLine("Downloading {0} to file {1}",
                        grp.Value, file);
                    //client.DownloadFile(new Uri(grp.Value), file);
                }
                catch
                {
                    Console.WriteLine("Failed to download {0}", grp.Value);
                }

            }
        }
    }
}
Downloading http://www.java2s.com/Code/Java/CatalogJava.htm to file CatalogJava.htm
Downloading http://www.java2s.com/Tutorial/Java/CatalogJava.htm to file CatalogJava.htm
Downloading http://www.java2s.com/Article/Java/CatalogJava.htm to file CatalogJava.htm
Downloading http://www.java2s.com/Product/Java/CatalogJava.htm to file CatalogJava.htm








33.18.WebClient
33.18.1.Use WebClient to download information into a file
33.18.2.Use WebClient to read website
33.18.3.Download string from a website using the WebClient
33.18.4.Download HTM files
33.18.5.Download File
33.18.6.Download Data
33.18.7.Upload Data
33.18.8.Open/Write Test
33.18.9.Web login
33.18.10.Open/Read Test
33.18.11.Response Headers
33.18.12.Upload Values
33.18.13.Set download string for WebClient