how to login in https sites with the help of webrequest and response
<p>how to login in https sites with the help of webrequst and webresponse in c# .</p>

<p>here is the code</p>

<pre><code>public string postFormData(Uri formActionUrl, string postData)
    {
        gRequest = (HttpWebRequest)WebRequest.Create(formActionUrl);
        gRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4";
        gRequest.CookieContainer = new CookieContainer();
        gRequest.Method = "POST";
        gRequest.Accept = " text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8, */*";
        gRequest.KeepAlive = true;
        gRequest.ContentType = @"text/html; charset=iso-8859-1";

        #region CookieManagement
        if (this.gCookies != null &amp;&amp; this.gCookies.Count &gt; 0)
        {
            gRequest.CookieContainer.Add(gCookies);
        }

        //logic to postdata to the form
        string postdata = string.Format(postData);
        byte[] postBuffer = System.Text.Encoding.GetEncoding(1252).GetBytes(postData);
        gRequest.ContentLength = postBuffer.Length;
        Stream postDataStream = gRequest.GetRequestStream();
        postDataStream.Write(postBuffer, 0, postBuffer.Length);
        postDataStream.Close();
        //post data logic ends

        //Get Response for this request url
        gResponse = (HttpWebResponse)gRequest.GetResponse();



        //check if the status code is http 200 or http ok
        if (gResponse.StatusCode == HttpStatusCode.OK)
        {
            //get all the cookies from the current request and add them to the response object cookies
            gResponse.Cookies = gRequest.CookieContainer.GetCookies(gRequest.RequestUri);
            //check if response object has any cookies or not
            if (gResponse.Cookies.Count &gt; 0)
            {
                //check if this is the first request/response, if this is the response of first request gCookies
                //will be null
                if (this.gCookies == null)
                {
                    gCookies = gResponse.Cookies;
                }
                else
                {
                    foreach (Cookie oRespCookie in gResponse.Cookies)
                    {
                        bool bMatch = false;
                        foreach (Cookie oReqCookie in this.gCookies)
                        {
                            if (oReqCookie.Name == oRespCookie.Name)
                            {
                                oReqCookie.Value = oRespCookie.Name;
                                bMatch = true;
                                break; // 
                            }
                        }
                        if (!bMatch)
                            this.gCookies.Add(oRespCookie);
                    }
                }
            }
        #endregion



            StreamReader reader = new StreamReader(gResponse.GetResponseStream());
            string responseString = reader.ReadToEnd();
            reader.Close();
            //Console.Write("Response String:" + responseString);
            return responseString;
        }
        else
        {
            return "Error in posting data";
        }
    }

// calling the above function

httphelper.postFormData(new Uri("https://login.yahoo.com/config/login?.done=http://answers.yahoo.com%2f&amp;.src=knowsrch&amp;.intl=us"), ".tries=1&amp;.src=knowsrch&amp;.md5=&amp;.hash=&amp;.js=&amp;.last=&amp;promo=&amp;.intl=us&amp;.bypass=&amp;.partner=&amp;.u=0b440p15q1nmb&amp;.v=0&amp;.challenge=Rt_fM1duQiNDnI5SrzAY_GETpNTL&amp;.yplus=&amp;.emailCode=&amp;pkg=&amp;stepid=&amp;.ev=&amp;hasMsgr=0&amp;.chkP=Y&amp;.done=http%3A%2F%2Fanswers.yahoo.com%2F&amp;.pd=knowsrch_ver%3D0%26c%3D%26ivt%3D%26sg%3D&amp;login=xyz&amp;passwd=xyz&amp;.save=Sign+In");
</code></pre>
