Last active
January 30, 2017 21:19
-
-
Save JohnRuddy/819ed163d07cb7e684035f50efb85f46 to your computer and use it in GitHub Desktop.
Check List of Status of List of Websites
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace WebsiteChecker | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var urls = new List<string>(); | |
// read in a series of websites | |
using (var reader = new StreamReader("WebsiteList.txt")) | |
{ | |
while (!reader.EndOfStream) | |
{ | |
urls.Add(reader.ReadLine()); | |
} | |
} | |
// Track the status of each site | |
using (FileStream fs = new FileStream("Results.txt", FileMode.Append, FileAccess.Write)) | |
using (StreamWriter sw = new StreamWriter(fs)) | |
{ | |
foreach (var url in urls) | |
{ | |
HttpWebResponse httpRes = null; | |
var httpReq = (HttpWebRequest)WebRequest.Create("http://" + url); | |
httpReq.AllowAutoRedirect = false; | |
try | |
{ | |
httpRes = (HttpWebResponse)httpReq.GetResponse(); | |
sw.WriteLine("{0} - {1}", url, httpRes.StatusCode); | |
httpRes.Close(); | |
} | |
catch (Exception) | |
{ | |
sw.WriteLine(url + " - Error"); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment