How to use HttpWebRequest to simulate Hotmail Login?
|
|
|
|
|
Downloads
If you are seeing this section and do not see download links, this means that you are not logged into our site. If you already are a member, click on the login link
and login into site and come back to this page for downloading the control files. If you are not a member, click on registration link to
become a Winista member and download the control for free.
Recently one of my clients asked for screen scrapping of a web site which required
login into the site first. My first reaction was, why not. We have built plenty
of custom screen scrapper for clients using out HTMLParser.Net.
When I started working on it, I realized that simple HttpWebRequest call was not returning the
output that we expected. HttpWebResponse object returned from the request method was
not returning any error either. Then I monitored the request in Http debugger proxy and saw that
when I performed login using browser, the request was being redirected to some other URL.
Now I realized that I also need to simulate the same process. By default HttpWebRequest automatically
redirects the calls to next URL and hides the fact that the actual request was redirected to some other
URL. If you want to simulate the process of redirection manually, you need to stop HttpWebRequest from
redirecting the request automatically. This is where AllowAutoRedirect property comes into
play. Default value of this property is set to true. So first thing that you need to do is
set this property value to false. And if your request requires cookies to passed with
every redirection, then you need to attach a CookieContainer object to every request. The
returned cookies need to be attached to subsequent requests.
Now you must be asking, how do I detect if request has been redirected and what is the next URL where
I should be sending request to. All this information is provided in HttpWebResponse object returned by
GetResponse method. StatusCode property value of response object is set to
HttpStatusCode.Found which is equivalent to HTTP code 302. And when this happens, the target
URL is returned in Location header of response.
A perfect example of this is login process of Hotmail account. Most of us simply click "Hotmail" button
in our MSN home page and not notice what is the final destination URL that displays the screen for
entering login credentials. The demo project contains a console application that demonstrates this whole
process in action. The code walks the target URLs till it gets the URL which does not require redirection.
do
{
webReq = WebRequest.Create(strUrl) as HttpWebRequest;
webReq.CookieContainer = cookies;
webReq.AllowAutoRedirect = false;
Debug.WriteLine("Hope[" + hops.ToString() + "] - " + strUrl);
Console.WriteLine("Hope[{0}] - {1}", hops++, strUrl);
HttpWebResponse webResp = webReq.GetResponse() as HttpWebResponse;
webResp.Close();
if (webResp.StatusCode == HttpStatusCode.Found)
{
strUrl = webResp.Headers["Location"] as String;
}
else
{
status = webResp.StatusCode;
bFoundDestination = (webResp.StatusCode == HttpStatusCode.OK);
break;
}
} while (true);
The following is output when I ran the attached demo project. You can see that
it took 3 redirections before reaching final URL where user needs to login.
Hope[0] - http://hotmail.msn.com
Hope[1] - http://lc2.bay0.hotmail.passport.com/cgi-bin/login
Hope[2] - http://www.hotmail.com
Hope[3] - http://login.live.com/login.srf&id=2&svc=mail&cbid=24325&
msppjph=1&tw=0&fs=1&fsa=1&fsat=1296000&lc=1033&_lang=EN
Netomatix team appreciates everybody's continued support and contribution to site.
Join The Effort
|