Skip to content

Instantly share code, notes, and snippets.

/ExWebClient.cs Secret

Created August 8, 2014 00:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d62d564370ba7523787a to your computer and use it in GitHub Desktop.
Save anonymous/d62d564370ba7523787a to your computer and use it in GitHub Desktop.
Spark Scraper
using System.IO;
using System.Net;
using HtmlAgilityPack;
namespace MySparkScraper
{
class ExWebClient
{
//The cookies will be here.
private CookieContainer _cookies = new CookieContainer();
//In case you need to clear the cookies
public void ClearCookies()
{
_cookies = new CookieContainer();
}
public HtmlDocument Get(string url)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Proxy = WebRequest.DefaultWebProxy;
request.Method = "GET";
//This is the important part.
request.CookieContainer = _cookies;
var response = (HttpWebResponse)request.GetResponse();
var stream = response.GetResponseStream();
//When you get the response from the website, the cookies will be stored
//automatically in "_cookies".
using (var reader = new StreamReader(stream))
{
string html = reader.ReadToEnd();
var doc = new HtmlDocument();
doc.LoadHtml(html);
return doc;
}
}
public HtmlDocument Login(string url, string username, string password)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Proxy = WebRequest.DefaultWebProxy;
byte[] send = System.Text.Encoding.Default.GetBytes(string.Format("loginSrc=ext&username={0}&password={1}&sign-in=Sign+In", username, password));
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = send.Length;
request.Method = "POST";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36";
request.Headers["Cache-Control"] = "no-cache";
//This is the important part.
request.CookieContainer = _cookies;
Stream sout = request.GetRequestStream();
sout.Write(send, 0, send.Length);
sout.Flush();
sout.Close();
var response = (HttpWebResponse)request.GetResponse();
var stream = response.GetResponseStream();
//When you get the response from the website, the cookies will be stored
//automatically in "_cookies".
using (var reader = new StreamReader(stream))
{
string html = reader.ReadToEnd();
var doc = new HtmlDocument();
doc.LoadHtml(html);
return doc;
}
}
}
}
using System;
using System.Linq;
using Newtonsoft.Json.Linq;
namespace MySparkScraper
{
class Program
{
static void Main(string[] args)
{
var client = new ExWebClient();
var doc1 = client.Get("https://www.spark.co.nz/myspark/");
// find the login url
var loginForm = doc1.DocumentNode.Descendants("form").FirstOrDefault(n => n.Attributes["name"] != null && n.Attributes["name"].Value == "myForm");
var loginUrl = loginForm.Attributes["action"].Value.Replace("&", "&");
client.Login(loginUrl, "<encoded email>", "<password>");
var usageInfo = client.Get("https://www.spark.co.nz/portal/site/mytelecom-site/template.BINARYPORTLET/menuitem.c26e41f6aeaa53d794910bf3bc407ea0/resource.process/?javax.portlet.tpst=e0282410b21e6a6e85cdc707d5107ea0&javax.portlet.rid_e0282410b21e6a6e85cdc707d5107ea0=ajaxGetDataUsage&javax.portlet.rcl_e0282410b21e6a6e85cdc707d5107ea0=cacheLevelPage&javax.portlet.begCacheTok=com.vignette.cachetoken&javax.portlet.endCacheTok=com.vignette.cachetoken");
dynamic info = JObject.Parse(usageInfo.DocumentNode.InnerText);
Console.WriteLine(string.Format("Used {0} of {1} {2} for period {3}", info.actionResult.dataUsed,
info.actionResult.dataCap, info.actionResult.dataUnit, info.actionResult.usagePeriod));
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment