开发者

how to login in https sites with the help of webrequest and response

开发者 https://www.devze.com 2022-12-22 21:33 出处:网络
how to login in https sites with the help of webrequst and webresponse in c# . here is the code public string postFormData(Uri formActionUrl, string postData)

how to login in https sites with the help of webrequst and webresponse in c# .

here is the 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 && this.gCookies.Count > 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 > 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&.src=knowsrch&.intl=us"), ".tries=1&.src=knowsrch&.md5=&.开发者_开发技巧hash=&.js=&.last=&promo=&.intl=us&.bypass=&.partner=&.u=0b440p15q1nmb&.v=0&.challenge=Rt_fM1duQiNDnI5SrzAY_GETpNTL&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=0&.chkP=Y&.done=http%3A%2F%2Fanswers.yahoo.com%2F&.pd=knowsrch_ver%3D0%26c%3D%26ivt%3D%26sg%3D&login=xyz&passwd=xyz&.save=Sign+In");


You need to see how authentication works for the site you are working with.

This may be through cookies, special headers, hidden field or something else.

  1. Fire up a tool like Fiddler and see what the network traffic is like when logging in and how it is different from not being logged in
  2. Recreate this logic with WebRequest and WebResponse.

See the answers to this SO question (HttpRequest: pass through AuthLogin).


What for? Watin is good for testing and such, and it's easy to do basic screen scraping with it. Why reinvent the wheel if you don't have to.


you can set the WebRequest.Credentials property. for an example and documentation see:

http://msdn.microsoft.com/en-us/library/system.net.networkcredential.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号