Publish Article

How to use HttpWebRequest to simulate Hotmail Login?


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.

	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;
	if (webResp.StatusCode == HttpStatusCode.Found)
		strUrl = webResp.Headers["Location"] as String;
		status = webResp.StatusCode;
		bFoundDestination = (webResp.StatusCode == HttpStatusCode.OK);
} 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&
Netomatix team appreciates everybody's continued support and contribution to site. Join The Effort


URL redirection is not working in all location
manish.knp   11/30/2009 12:15:30 PM

Hi below mention is the url that is further redirected to other site, when i use ur code for below mention url its not working. but when we use for "hotmail.com" ur mention url it's working could you pls tell me what is the reasion? thanks in advance, need your feedback asap pls. url="http://legal-tb.auto-graphics.com/loginModule/Referrer.aspx?myses=16965&w=S&k=712481&cuid=legal&cusrvr=cassiopia&s=wdb<is=4<ype=f&lmethod=1001&url=https%3a%2f%2fwww.lexis.com%2fresearch%2fretrieve%3f_m%3dd8ca55f1090c4475c89026e5fbc07401%26docnum%3d4%26_fmtstr%3dFULL%26_startdoc%3d1%26wchp%3ddGLbVzz-zSkAz%26_md5%3de4dd13f6ce45966fc41fc00c665ad0c1&bof=0&clientId=ACME-ACME2-C1235" Regards Manish

RE:login to hotmail from my webpage
xiaoyiily   7/24/2008 5:26:19 AM

I also want to know,anyone else can help us? 

login to hotmail from my webpage
crimsondesign   5/3/2008 9:31:33 PM

all i want to know is how to be able to sign in to hotmail with out having to go to hotmail.com. just put in my e-mail and password in fields on my own personal webpage and login from there. if anyone can help me please do. im begging

RE:Redirections via JavaScript
madrasprogrammer   1/11/2008 5:24:37 AM

Daear NK,

If the site is accessible through browser means you can access it through C#. All you need that is microsoft Fiddler and knowledge of HTTP protocol. you can get the complete source code at:
and article at:

love zvasanth

RE:Redirections via JavaScript
nalica   7/2/2007 11:41:08 PM


Does anybody have a more complete code to simulate login and retrieve result after login?

I am new to this and I do not understand where to attach the username and password to HttpWebRequest or where to put HttpWebResponse code to get the result after login simulation.



RE:Redirections via JavaScript
mandark   11/22/2006 11:45:36 PM


Can you please give me the code as to how you have done the login to gmail and read all mails using .net code

Thanks in advance

Mandar K


RE:login to hotmail
mandark   11/22/2006 11:43:20 PM

how the code mentioned on this page will help us to login to hotmail using .net code.

Please help me




RE:Redirections via JavaScript
devrex   11/10/2006 11:50:32 PM

I tried the google friend extractor and it works great.  I want to try and read mails and orkut mesages from my peronal homepage. Could you post the code for simulating login to google like you have for hotmail or mail me at devrex@hotmail.com

Thanks in advance!

RE:Redirections via JavaScript
NetoMatix   11/9/2006 7:11:27 AM

Login into Orkut and GMail is same because they both get authenticated from google accounts. Yes we did simulate the loging into Gmail and Orkut account. Please see the following link for our demo on google friend list extraction

RE:Redirections via JavaScript
mandark   11/8/2006 6:08:45 AM

how to send data i.e user name and password to gmail a/c and read mails from that.


Thanks in advancd


Mandar k

RE:Redirections via JavaScript
devrex   10/31/2006 9:48:00 PM

Have you been able to find a solution for using httpwebrequest to login to Orkut? Please let eme know w\how this can be done


RE:Redirections via JavaScript
NetoMatix   9/7/2006 1:56:43 PM

Unfortunately we do not have an invite for orkut. Otherwise we can look into this issue for you. If you want, you can send an orkut invite to winista AT gmail DOT com and we will simulate the situation.

This sample was just to give an idea how HttpWebRequest can be used for redirection. Using this technique we have been able to simulate login into Gmail account and get all the emails.

Redirections via JavaScript
joseamericoantoine   9/6/2006 3:53:16 PM

I thought very interesting.

But, when i tried to do a web login in orkut i discovered their login works with javascript redirection

In this case this script doesnt serve

If anyone knows a solution for this issue pls tell me


Go Freelance
Home     About us     Contact us    Copyright    Privacy Policy    Return Policy    Advertisers
Copyright © Netomatix