开发者

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

This question already has answers here: C# .NET Cookie Handling For After Login (2 answers) Closed 3 years ago. 开发者_运维问答

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.


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);


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜