Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Net;
- namespace CheckUserNames
- {
- class Program
- {
- static void Main(string[] args)
- {
- //Declare input and output file
- string InputFilePath = @"C:\b\input.txt";
- string OutputFilePath = @"C:\b\output.txt";
- //Read input file
- var input = new StreamReader(InputFilePath);
- string CurrentUsername;
- //Open output file for writing, overwrite if it already exists.
- //Change false to true to append instead of overwriting.
- var Output = new StreamWriter(OutputFilePath, false);
- Console.WriteLine("Reading users...");
- //Read every line of the input file.
- while (!input.EndOfStream)
- {
- CurrentUsername = input.ReadLine();
- //Check if username is available
- if (UserNameExists(CurrentUsername))
- {
- Console.WriteLine("Username {0} exists", CurrentUsername);
- }
- else
- {
- Console.WriteLine("Username {0} doesn't exist", CurrentUsername);
- //Output to file if it is
- Output.WriteLine(CurrentUsername);
- }
- }
- //Cleanup
- Output.Close();
- input.Close();
- Console.WriteLine("Done. Press Enter to exit...");
- Console.ReadLine();
- }
- /// <summary>
- /// Check to see if a username exists
- /// </summary>
- /// <param name="username">The username</param>
- /// <returns>True if the username exists. False if not.</returns>
- private static bool UserNameExists(string username)
- {
- try
- {
- string url = "http://www.reddit.com/user/" + username;
- HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
- request.Method = "HEAD";
- request.Timeout = 2000; // I've noticed it can take very long to get a timeout. So I set it to two seconds.
- HttpWebResponse response = request.GetResponse() as HttpWebResponse;
- return (response.StatusCode == HttpStatusCode.OK);
- }
- catch
- {
- return false;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment