How do I need to read/know in order to log into a site and perform an action? [duplicate]

为君一笑 提交于 2019-12-23 04:01:55

问题


This is a mini project that has been tasked to me as part of a 'teach myself to code marathon'. I need to

  1. Log into a website that requires a user/pass (e.g facebook) and return the response. I have managed to achieve that using WebRequest and WebResponse classes.
  2. The next step is to send a message to my tutor programmatically

This is where I am stumped. How do I access the 'send message' functionality of the site? I imagine I need to first look for the query parameters which I can do so using firebug but I am confused as to how can I persist my 'logged in status' and send the message? I've been searching around for a bit and I think it involves cookies but Im not sure how to proceed.

Thanks.


回答1:


you need the CookieCollection; after receiving the response from your login request you can read the cookies out of the response.

        var cookies = new CookieContainer();
        ServicePointManager.Expect100Continue = false;
        CookieCollection receivedCookies = new CookieCollection();

        try
        {
            var request = (HttpWebRequest) WebRequest.Create(ServerUrl + "index.php");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = cookies;

            string postData = "try=&username=someLogin&password=somepass";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = byteArray.Length;

            using (Stream dataStream = request.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
            }

            // Get the response.
            using (WebResponse response = request.GetResponse())
            {
                receivedCookies = ((HttpWebResponse) response).Cookies;
                Logger.DebugFormat("received {0} cookies", receivedCookies.Count);
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        string responseFromServer = reader.ReadToEnd();
                        Logger.DebugFormat("response from server after login-post: {0}", responseFromServer);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Logger.FatalFormat("there was an exception during login: {0}", ex.Message);
            return (int) CreateResult.GenericError;
        }

during sub-sequent requests you have to add always that cookie(s):

            var request =
                (HttpWebRequest)WebRequest.Create(ServerUrl + "index.php?nav=callcenter&sub=ccagents&action=new");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = cookies;
            request.CookieContainer.Add(receivedCookies);



回答2:


When you log in, the Response will contain one or more cookies that you will need to send in any subsequent requests to the server.



来源:https://stackoverflow.com/questions/4555334/how-do-i-need-to-read-know-in-order-to-log-into-a-site-and-perform-an-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!