1

在这上面花了一个晚上的大部分时间,但不知道出了什么问题。数据被发布到登录页面并得到很好的处理 - 错误的登录/密码会作为错误页面出现,成功的登录/密码会得到一个带有“位置”标题的空白响应。但是,尝试去那里或任何其他受保护的页面都会返回登录页面。我想我一定是在某处遗漏了一些小东西。下面是完整的代码,当然少了实际的用户/密码。我希望有人知道出了什么问题:)

$class Program
{
    static void Main(string[] args)
    {
        try
        {
            var cookieContainer = new CookieContainer();
            var username = "user";
            var password = "pwd";

            // LOGIN
            Console.WriteLine("Logging in...");

            var postData = string.Format("username={0}&password={1}&remote=&action=auth&submit=Login&from=", username, password);
            var req = (HttpWebRequest)WebRequest.Create("https://freedns.afraid.org/zc.php");
            req.CookieContainer = cookieContainer;
            req.Method = "POST";
            req.ContentLength = postData.Length;
            req.ContentType = "application/x-www-form-urlencoded";

            // This is necessary to capture cookies
            // The response will contain a "Location" header for redirect
            req.AllowAutoRedirect = false;

            req.KeepAlive = true;
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";

            var encoding = new ASCIIEncoding();
            var loginDataBytes = encoding.GetBytes(postData);
            req.ContentLength = loginDataBytes.Length;
            var stream = req.GetRequestStream();
            stream.Write(loginDataBytes, 0, loginDataBytes.Length);

            var webResp = (HttpWebResponse)req.GetResponse();

            var datastream = webResp.GetResponseStream();
            if (datastream == null) { throw new Exception("Data stream is null."); }

            var reader = new StreamReader(datastream);
            var response = reader.ReadToEnd();

            if (response.IndexOf("Invalid UserID/Pass") != -1) { throw new Exception("Invalid user/password."); }
            if (response.IndexOf("must enable cookies") != -1) { throw new Exception("Cookies not enabled."); }

            // ACCESS PAGE
            Console.WriteLine("Accessing page...");

            req = (HttpWebRequest)WebRequest.Create("https://freedns.afraid.org/dynamic/");
            req.CookieContainer = cookieContainer;
            req.Method = "GET";
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";

            webResp = (HttpWebResponse)req.GetResponse();

            datastream = webResp.GetResponseStream();
            if (datastream == null) { throw new Exception("No response received."); }

            reader = new StreamReader(datastream);
            response = reader.ReadToEnd();
            Console.WriteLine(response);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            Console.ReadKey();
        }
    }
}
4

1 回答 1

0

您可能需要使用HttpUtility.UrlEncode(string)才能发布特殊字符,例如!@#$%^&*()_+<>?:;"'{[}]|\

您需要将其添加为对项目的引用,并在命名空间中引用它。

于 2015-01-14T23:24:31.470 回答