<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Eclipsed4utoo&#039;s Blog&#187; programmatically</title>
	<atom:link href="http://eclipsed4utoo.com/blog/tag/programmatically/feed/" rel="self" type="application/rss+xml" />
	<link>http://eclipsed4utoo.com/blog</link>
	<description>Not Your Ordinary Programmer</description>
	<lastBuildDate>Thu, 08 Sep 2011 17:09:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>C# &#8211; Log in to Website Programmatically</title>
		<link>http://eclipsed4utoo.com/blog/log-website-programmatically/</link>
		<comments>http://eclipsed4utoo.com/blog/log-website-programmatically/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 22:00:41 +0000</pubDate>
		<dc:creator>Ryan Alford</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[programmatically]]></category>
		<category><![CDATA[WebBrowser]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://eclipsed4utoo.com/blog/?p=244</guid>
		<description><![CDATA[In this tutorial, will show how to log into a website through code.  I am going to use Twitter in my example. THIS IS FOR EDUCATIONAL PURPOSES ONLY.  I WOULD NOT ADVISE THE USE OF THIS TO ALWAYS LOG-IN TO TWITTER. First, you will need to download and install Tamper Data.  It&#8217;s an add-on for [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, will show how to log into a website through code.  I am going to use Twitter in my example.</p>
<p><span style="color: #ff0000;"><strong>THIS IS FOR EDUCATIONAL PURPOSES ONLY.  I WOULD NOT ADVISE THE USE OF THIS TO ALWAYS LOG-IN TO TWITTER.</strong></span></p>
<p>First, you will need to download and install <a href="https://addons.mozilla.org/en-US/firefox/addon/966" target="_blank">Tamper Data</a>.  It&#8217;s an add-on for Firefox that allows you to view and tamper with GET/POST web request data.  We won&#8217;t be doing any tampering.  We will be using it to view what POST parameters the page is expecting.</p>
<p>Next, navigate to the log in page for Twitter(<a href="https://twitter.com/login" target="_blank">http://twitter.com/login</a>).  Once the page has loaded, go to <strong>Tools</strong> &#8211;&gt; <strong>Tamper Data</strong> to open <strong>Tamper Data</strong>.  At the top of Tamper Data, click the <strong>Start Tamper</strong> button.  After clicking the button, click the &#8220;Sign In&#8221; button on Twitter&#8217;s log in page.  Once you hit the Sign In button, Tamper Data will prompt you with this popup&#8230;.</p>
<p><img class="alignnone size-full wp-image-245" title="Tamper Data Popup" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2009/12/12-9-2009-4-24-49-PM.png" alt="Tamper Data Popup" width="324" height="140" /></p>
<p>Click &#8220;Tamper&#8221;.  You will then be presented with this window&#8230;</p>
<p><img class="alignnone size-full wp-image-246" title="Tamper Data" src="http://eclipsed4utoo.com/blog/wp-content/uploads/2009/12/12-9-2009-4-15-09-PM.png" alt="Tamper Data" width="579" height="391" /></p>
<p>If you notice on the right-hand side of the window, you will see the POST parameters.  These are:</p>
<p>authenticity_token<br />
session[username_or_email]<br />
session[password]<br />
commit</p>
<p>Now that we have those, we can close the Tamper Data window, and close Firefox.</p>
<p>Now for the code.  The code is actually fairly simple.  We are just going to use the <strong>WebBrowser</strong> class to make the requests to the server to get the html source of the page.  This will give us the &#8220;authenticity_token&#8221; for us to use in the POST request.</p>
<pre class="brush: csharp;">
string url = &quot;https://twitter.com/login&quot;;
string username = &quot;someUserName&quot;;
string password = &quot;somePassword&quot;;
string commit = &quot;Sign+In&quot;; //this matches the data from Tamper Data

private void Login()
{
     WebBrowser b = new WebBrowser();
     b.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted);
     b.Navigate(url);
}

private void b_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
     WebBrowser b = sender as WebBrowser;
     string response = b.DocumentText;

     // looks in the page source to find the authenticity token.
     // could also use regular expressions here.
     int index = response.IndexOf(&quot;authenticity_token&quot;);
     int startIndex = index + 41;
     string authenticityToken = response.Substring(startIndex, 40);

     // unregisters the first event handler
     // adds a second event handler
     b.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted);
     b.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted2);

     // format our data that we are going to post to the server
     // this will include our post parameters.  They do not need to be in a specific
     //    order, as long as they are concatenated together using an ampersand ( &amp; )
     string postData = string.Format(&quot;authenticity_token={2}&amp;session[username_or_email]={0}&amp;session[password]={1}&amp;commit={3}&quot;, username, password, authenticityToken, commit);

     ASCIIEncoding enc = new ASCIIEncoding();

     //  we are encoding the postData to a byte array
     b.Navigate(&quot;https://twitter.com/sessions&quot;, &quot;&quot;, enc.GetBytes(postData), &quot;Content-Type: application/x-www-form-urlencoded\r\n&quot;);
}

private void b_DocumentCompleted2(object sender, WebBrowserDocumentCompletedEventArgs e)
{
     WebBrowser b = sender as WebBrowser;
     string response = b.DocumentText;

     if (response.Contains(&quot;Sign out&quot;))
     {
         MessageBox.Show(&quot;Login Successful&quot;);
     }
}
</pre>
<p>And that&#8217;s all you need to do.  You can now use the <strong>response</strong> variable to see the tweets that are in your timeline.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclipsed4utoo.com/blog/log-website-programmatically/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>

