Download from URL and parse content - CSharp Network

CSharp examples for Network:URL

Description

Download from URL and parse content

Demo Code

using System;/*from w  w  w .j ava2s .  c o m*/
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
class MainClass
{
   private static void Main()
   {
      string srcUriString = "http://www.book2s.com";
      Uri srcUri = new Uri(srcUriString);
      WebClient client = new WebClient();
      Console.WriteLine("Downloading {0}", srcUri);
      string str = client.DownloadString(srcUri);
      MatchCollection matches = Regex.Matches(str,"<img.*?src\\s*=\\s*[\"'](?<url>.*?\\.(gif|jpg|jpeg)).*?>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
      foreach(Match match in matches)
      {
         var urlGrp = match.Groups["url"];
         if (urlGrp != null && urlGrp.Success)
         {
            Uri imgUri = null;
            if (urlGrp.Value.StartsWith("http"))
            {
               imgUri = new Uri(urlGrp.Value);
            }
            else if (urlGrp.Value.StartsWith("/"))
            {
               imgUri = new Uri(srcUri, urlGrp.Value);
            }
            else
            {
               Console.WriteLine("Skipping image {0}", urlGrp.Value);
            }
            if (imgUri != null)
            {
               string fileName = urlGrp.Value.Substring(urlGrp.Value.LastIndexOf('/')+1);
               try
               {
                  Console.WriteLine("Downloading {0} to {1}", imgUri.AbsoluteUri, fileName);
                  client.DownloadFile(imgUri, fileName);
               }
               catch
               {
                  Console.WriteLine("Failed to download {0}", imgUri.AbsoluteUri);
               }
            }
         }
      }
   }
}

Result


Related Tutorials