我需要如何阅读/知道才能登录站点并执行操作? [英] How do I need to read/know in order to log into a site and perform an action?

查看:63
本文介绍了我需要如何阅读/知道才能登录站点并执行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个微型项目,已作为自学马拉松代码"的一部分向我分配任务.我需要

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

  1. 登录需要用户/通行证的网站(例如facebook)并返回响应.我设法使用WebRequestWebResponse类实现了这一目标.
  2. 下一步是以编程方式向老师发送消息
  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

这是我感到难过的地方.如何访问网站的发送消息"功能?我想我需要先查找可以使用Firebug进行查询的查询参数,但是我对如何保持登录状态"并发送消息感到困惑.我一直在寻找一些信息,我认为它涉及cookie,但是我不确定如何进行.

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.

谢谢.

推荐答案

您需要CookieCollection;在收到登录请求的响应后,您可以从响应中读取cookie.

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

在后续请求期间,您必须始终添加该cookie:

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

这篇关于我需要如何阅读/知道才能登录站点并执行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆