Check the ContentType

   



using System;
using System.IO;
using System.Net;
   
class HtmlDump
{
     public static int Main(string[] astrArgs)
     {
          WebRequest webreq;
          WebResponse webres;
   
          try
          {
               webreq = WebRequest.Create("http://www.kutayzorlu.com/java2s/com/");
               webres = webreq.GetResponse();
          }
          catch (Exception exc)
          {
               Console.WriteLine("HtmlDump: {0}", exc.Message);
               return 1;
          }
   
          if (webres.ContentType.Substring(0, 4) != "text")
          {
               Console.WriteLine("HtmlDump: URI must be a text type.");
               return 1;
          }
   
          Stream       stream = webres.GetResponseStream();
          StreamReader strrdr = new StreamReader(stream);
          string       strLine;
   
          while ((strLine = strrdr.ReadLine()) != null){
               Console.WriteLine(strLine);
          }
          stream.Close();
          return 0;
     }
}


           
          


Create GetResponse from WebRequest

   


using System;
using System.Net;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

public class MainClass {
    public static void Main() {
        string picUri = "http://international.us.server12.fileserver.kutayzorlu.com/files/download/2017/01/Hex_RGB4_uuid-fa046b6c-fa2c-4f3e-838c-80ed56faee5c_crc-0.jpg";
        string htmlUri = "http://www.apress.com";

        WebRequest requestPic = WebRequest.Create(picUri);
        WebRequest requestHtml = WebRequest.Create(htmlUri);

        WebResponse responsePic = requestPic.GetResponse();
        WebResponse responseHtml = requestHtml.GetResponse();

        Image img = Image.FromStream(responsePic.GetResponseStream());

        using (StreamReader r = new StreamReader(responseHtml.GetResponseStream())) {
            Console.WriteLine(r.ReadToEnd());
        }
    }
}
           
          


Output webpage content

   



using System.Net;
using System;
using System.IO;
public class WebPagesApp {
    [STAThread]
    public static void Main(string[] args) {
        string s = "http://www.microsoft.com";
        Uri uri = new Uri(s);
        WebRequest req = WebRequest.Create(uri);
        WebResponse resp = req.GetResponse();
        Stream str = resp.GetResponseStream();
        StreamReader sr = new StreamReader(str);

        string t = sr.ReadToEnd();
        int i = t.IndexOf("<HEAD>");
        int j = t.IndexOf("</HEAD>");
        string u = t.Substring(i, j);
        Console.WriteLine("{0}", u);
    }
}

           
          


Download a web page in a thread

   


using System;
using System.Net;
using System.Threading;

class ThreadTest {
    static void Main() {
        new Thread(Download).Start();
        Console.WriteLine("download&#039;s happening!");
        Console.ReadLine();
    }

    static void Download() {
        using (WebClient wc = new WebClient())
            try {
                wc.Proxy = null;
                wc.DownloadFile("http://www.google.com", "index.html");
                Console.WriteLine("Finished!");
            } catch (Exception ex) {
            }
    }
}
           
          


Build the DownloadString

   





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

class MainClass {
    private static void Main() {
        string remoteUri = "http://www.apress.com";
        WebClient client = new WebClient();
        string str = client.DownloadString(remoteUri);
        MatchCollection matches = Regex.Matches(str, @"httpS+[^-,;:?].gif");
        foreach (Match match in matches) {
            foreach (Group grp in match.Groups) {
                string file = grp.Value.Substring(grp.Value.LastIndexOf(&#039;/&#039;) + 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);
                }
            }
        }
    }
}
           
          


Set the BaseAddress for WebClient

   


using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

class Program {
    static void Main(string[] args) {
        WebClient client = new WebClient();
        client.BaseAddress = "http://www.microsoft.com";
        string data = client.DownloadString("Office");
        Console.WriteLine(data);

    }
}

           
          


Web Client Open Read Test

   


using System;
using System.IO;
using System.Net;

public class OpenReadTest
{
   public static void Main(string[] argv)
   {
      WebClient wc = new WebClient();
      string response;

      Stream strm = wc.OpenRead(argv[0]);

      StreamReader sr = new StreamReader(strm);

      while(sr.Peek() > -1)
      {
         response = sr.ReadLine();
         Console.WriteLine(response);
      }
      sr.Close();
   }
}