Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 10th, 2013  |  syntax: C#  |  size: 2.21 KB  |  views: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Net;
  8.  
  9. namespace CheckUserNames
  10. {
  11.   class Program
  12.   {
  13.  
  14.     static void Main(string[] args)
  15.     {
  16.       //Declare input and output file
  17.       string InputFilePath = @"C:\b\input.txt";
  18.       string OutputFilePath = @"C:\b\output.txt";
  19.  
  20.  
  21.       //Read input file
  22.       var input = new StreamReader(InputFilePath);
  23.       string CurrentUsername;
  24.  
  25.       //Open output file for writing, overwrite if it already exists.
  26.       //Change false to true to append instead of overwriting.
  27.       var Output = new StreamWriter(OutputFilePath, false);
  28.  
  29.       Console.WriteLine("Reading users...");
  30.      
  31.       //Read every line of the input file.
  32.       while (!input.EndOfStream)
  33.       {
  34.         CurrentUsername = input.ReadLine();
  35.  
  36.         //Check if username is available
  37.         if (UserNameExists(CurrentUsername))
  38.         {
  39.           Console.WriteLine("Username {0} exists", CurrentUsername);
  40.          
  41.           //Output to file if it is
  42.           Output.WriteLine(CurrentUsername);
  43.         }
  44.         else
  45.         {
  46.           Console.WriteLine("Username {0} doesn't exist", CurrentUsername);
  47.         }
  48.        
  49.       }
  50.  
  51.       //Cleanup
  52.       Output.Close();
  53.       input.Close();
  54.       Console.WriteLine("Done. Press Enter to exit...");
  55.       Console.ReadLine();
  56.     }
  57.  
  58.     /// <summary>
  59.     /// Check to see if a username exists
  60.     /// </summary>
  61.     /// <param name="username">The username</param>
  62.     /// <returns>True if the username exists. False if not.</returns>
  63.     private static bool UserNameExists(string username)
  64.     {
  65.       try
  66.       {
  67.         string url = "http://www.reddit.com/user/" + username;
  68.         HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
  69.         request.Method = "HEAD";
  70.         request.Timeout = 2000; // I've noticed it can take very long to get a timeout. So I set it to two seconds.
  71.         HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  72.         return (response.StatusCode == HttpStatusCode.OK);
  73.       }
  74.       catch
  75.       {
  76.         return false;
  77.       }
  78.     }
  79.   }
  80. }