首页 > 解决方案 > 获取 CSRF 令牌以强制登录,但它不返回任何内容 - ArgumentOutOfRangeException:长度不能小于零

问题描述

我正在尝试从大学获取一些课程数据,例如时间和地点。我目前正在尝试获取 CSRF 令牌,然后使用它登录。但是当我尝试使用一些简单的统一按钮获取 htmldata 时,它不会返回任何内容。ArgumentOutOfRangeException:长度不能小于零。参数名称:长度

有人可以帮助我解决潜在的问题吗?并可能指出为什么它不返回任何 html 数据?

谢谢阅读

    {
        public Text responseText;
        public void Button()
        {
            System.Net.CookieContainer myCookies = new System.Net.CookieContainer();
            string mySrc = HttpMethod.Get("https://signon.ruc.dk/login?service=https%3A%2F%2Fmoodle.ruc.dk%2Flogin%2Findex.php", "https://signon.ruc.dk/login?service=https%3A%2F%2Fmoodle.ruc.dk%2Flogin%2Findex.php", ref myCookies);
            string token = GetBetween(mySrc, "name=\"execution\" value=\"" , "=\">");
          //  string username = "XXX";
          //  string password = "XXX";

            string postData = "username=" + "XXX" + "&password=" + "XXX" + "&submit=Sign-on&execution=" + token;
            bool result = HttpMethod.Post("https://signon.ruc.dk/login?service=https%3A%2F%2Fmoodle.ruc.dk%2Flogin%2Findex.php", postData, "https://signon.ruc.dk/login?service=https%3A%2F%2Fmoodle.ruc.dk%2Flogin%2Findex.php", myCookies);
            if (result)
               responseText.text = result.ToString();
            else
                Debug.Log("Invalid!");
        }
        static string GetBetween(string msg, string start, string stop)
        {
            int StartIndex = msg.IndexOf(start) + start.Length;
            int StopIndex = msg.IndexOf(stop);
            return msg.Substring(StartIndex, StopIndex - StartIndex);```

标签: c#web-scrapingcsrf

解决方案


我相信您看到的异常是指出结果StopIndex - StartIndex小于零。这可能是有道理的,因为没有什么限制对子字符串的搜索在stop子字符串的末尾之后start。你是不是想说:

int StopIndex = msg.IndexOf(stop, StartIndex);

推荐阅读