private static string GetAccessToken(string userName, string password)
{
string retVal = null;
CookieContainer cookies = new CookieContainer();
string HTML = null;
string postUrl = null;
string nextRequestBody = null;
// Build and make the initial get request
HttpWebRequest httpRequest = BuildInitialSiteRequest();
using (HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse())
{
ExtractResponseCookiesToCookieContainer(ref cookies, response, 11, "http://login.live.com");
HTML = GetHTMLFromResponse(response);
nextRequestBody = BuildTokenPostFromInitialSiteRequest(HTML, userName, password, out postUrl);
}
//POST to the specified URI with the appropraite cookies
HttpWebRequest httpRequest2 = BuildPostRequest(ref cookies, postUrl, nextRequestBody);
// Get the response
using (HttpWebResponse response2 = (HttpWebResponse)httpRequest2.GetResponse())
{
// Get the html
HTML = GetHTMLFromResponse(response2);
ExtractResponseCookiesToCookieContainer(ref cookies, response2, 8, "http://login.live.com");
nextRequestBody = BuildPostBodyFromSecondRequest(HTML);
}
//POST back to the initial uri with the post body from the 2nd request
HttpWebRequest httpRequest3 = BuildThirdRequest(cookies, httpRequest, nextRequestBody);
using (HttpWebResponse response3 = (HttpWebResponse)httpRequest3.GetResponse())
{
HTML = GetHTMLFromResponse(response3, true);
ExtractResponseCookiesToCookieContainer(ref cookies, response3, 12, "http://login.live.com");
}
return retVal;
}