httpwebrequest/httpwebresponse使用cookiecontainer未登录



晚上大部分时间都花在了这方面,无法弄清楚怎么了。数据将发布到登录页面并进行处理良好 - 错误的登录/PWD作为错误页面出现,并且成功的页面带有"位置"标头获得空白响应。但是,试图去那里或任何其他受保护的页面都会返回登录页面。我认为我必须在某个地方错过一些小事。下面是完整的代码,当然是实际的用户/PWD。我希望有人对什么问题有所了解:)

$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();
        }
    }
}

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

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

相关内容

  • 没有找到相关文章

最新更新