Advertisement
SlothNGU

Instagram Username Checker

Feb 11th, 2018
8,589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Threading.Tasks;
  7. using Newtonsoft.Json;
  8.  
  9. public class Instagram
  10. {
  11.     private async Task<bool> checkUsername(string username)
  12.     {
  13.         /// <summary>Setup the HttpClient, HttpHandler & CookieContainer</summary>
  14.         CookieContainer instaCookies = new CookieContainer();
  15.         using (HttpClientHandler Handler = new HttpClientHandler() { CookieContainer = instaCookies, UseCookies = true, AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip })
  16.         using (HttpClient Client = new HttpClient(Handler))
  17.         {
  18.             /// <summary>Set the referer header for our requests as Instragram checks to see if it exists</summary>
  19.             Client.DefaultRequestHeaders.Add("referer", "https://www.instagram.com/");
  20.  
  21.             /// <summary>Set up the post data for when we send the account creation dry run to instagram</summary>
  22.             Dictionary<string, string> instagramPostData = new Dictionary<string, string>
  23.             {
  24.                 { "email", "testemail@testemail.test" },
  25.                 { "password", "testpassword" },
  26.                 { "username", username },
  27.                 { "first_name", "testname" }
  28.             };
  29.  
  30.             /// <summary>Make a GET request to Instagrams login page to set the site cookies inside our cookie container</summary>
  31.             HttpRequestMessage getInstaMsg = new HttpRequestMessage() { Method = HttpMethod.Get, RequestUri = new Uri("https://www.instagram.com/accounts/login/") };
  32.             HttpResponseMessage getInstaResponse = await Client.SendAsync(getInstaMsg);
  33.             string instaResponseString = await getInstaResponse.Content.ReadAsStringAsync();
  34.  
  35.             /// <summary>Retrieve the csrftoken cookie from the cookie container and set it as a header as Instragram checks this too</summary>
  36.             IEnumerable<Cookie> responseCookies = Handler.CookieContainer.GetCookies(new Uri("https://www.instagram.com")).Cast<Cookie>();
  37.             Client.DefaultRequestHeaders.Add("x-csrftoken", responseCookies.FirstOrDefault(x => x.Name == "csrftoken").Value);
  38.  
  39.             /// <summary>Make a POST request to Instagrams dry run account creation endpoint</summary>
  40.             HttpRequestMessage postInstaCheck = new HttpRequestMessage() { Method = HttpMethod.Post, RequestUri = new Uri("https://www.instagram.com/accounts/web_create_ajax/attempt/"), Content = new FormUrlEncodedContent(instagramPostData) };
  41.             getInstaResponse = await Client.SendAsync(postInstaCheck);
  42.  
  43.             /// <summary>Deserialze the JSON response from Instragram in to a Object</summary>
  44.             instagramRootObject instagramResponse = JsonConvert.DeserializeObject<instagramRootObject>(await getInstaResponse.Content.ReadAsStringAsync());
  45.  
  46.             /// <summary>Return a valid response if the dry run passed else return a invalid response</summary>
  47.             if (instagramResponse.dryrun_passed)
  48.                 return true;
  49.             else
  50.                 return false;
  51.         }
  52.     }
  53. }
  54. public class Email
  55. {
  56.     public string message { get; set; }
  57.     public string code { get; set; }
  58. }
  59.  
  60. public class Username
  61. {
  62.     public string message { get; set; }
  63.     public string code { get; set; }
  64. }
  65.  
  66. public class Password
  67. {
  68.     public string message { get; set; }
  69.     public string code { get; set; }
  70. }
  71.  
  72. public class Errors
  73. {
  74.     public List<Email> email { get; set; }
  75.     public List<Username> username { get; set; }
  76.     public List<Password> password { get; set; }
  77. }
  78.  
  79. public class instagramRootObject
  80. {
  81.     public bool account_created { get; set; }
  82.     public Errors errors { get; set; }
  83.     public bool dryrun_passed { get; set; }
  84.     public List<object> username_suggestions { get; set; }
  85.     public string status { get; set; }
  86.     public string error_type { get; set; }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement