Skip to content

Examples

Jason Gallagher edited this page Aug 13, 2018 · 1 revision

BrowseSharp Example Usage

Social media automated logon

This example demonstrates how you can automate the logon process for a popular social media platform. The example utilizes the javascript engine to abstract a javascript object containing a csrf token need for logon. This csrf token is then added to the header and the username and password are added to the formdata and submitted. Once submitted a json response will be returned indicating whether you have successfully logged in.


  /* Set username and password */
  string userName = "UserNameHere"; /* Put your username here */
  string password = "PasswordHere"; /* Put your password here */
            
  /* Initialize BrowseSharp browser */
  BrowseSharp.Browser browser = new BrowseSharp.Browser();
            
  /* Navigate to homepage to get csrf token */
  browser.Navigate("http://www.instagram.com");
            
  /* Add executable script to script engine */
  browser.JavascriptEngine.Document = browser.Documents[0].Scripts[3].JavascriptString;
            
  /* Get csrf token from javascript */
  dynamic result = (ExpandoObject) browser.JavascriptEngine.Execute("window._sharedData");
            
  /* Set form data */
  Dictionary<string,string> logonFormData = new Dictionary<string, string>();
  logonFormData.Add("username",userName);
  logonFormData.Add("password", password);
            
  /* Set headers */
  Dictionary<string,string> headers = new Dictionary<string, string>();
  headers.Add("X-CSRFToken", result.config.csrf_token);
            
  /* Logon */
  IDocument document = browser.Submit("https://www.instagram.com/accounts/login/ajax/", logonFormData, headers);
            

Clone this wiki locally