Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 使用CookieContainer的HttpWebRequest/HttpWebResponse未登录_.net_Cookies_Login_Httpwebrequest_Httpwebresponse - Fatal编程技术网

.net 使用CookieContainer的HttpWebRequest/HttpWebResponse未登录

.net 使用CookieContainer的HttpWebRequest/HttpWebResponse未登录,.net,cookies,login,httpwebrequest,httpwebresponse,.net,Cookies,Login,Httpwebrequest,Httpwebresponse,在这件事上花了一个晚上的大部分时间,却不知道出了什么问题。数据被发布到登录页面并得到很好的处理——错误的登录/pwd显示为错误页面,成功的登录会得到带有“Location”标题的空白响应。但是,如果尝试转到该页面或任何其他受保护的页面,则会返回登录页面。我想我一定是错过了什么小东西。下面是完整的代码减去实际用户/pwd。我希望有人知道出了什么问题:) 您可能需要使用HttpUtility.UrlEncode(string)来发布特殊字符,如!@$%^&*()_+?:;“{[}]\ 您需要将此添加

在这件事上花了一个晚上的大部分时间,却不知道出了什么问题。数据被发布到登录页面并得到很好的处理——错误的登录/pwd显示为错误页面,成功的登录会得到带有“Location”标题的空白响应。但是,如果尝试转到该页面或任何其他受保护的页面,则会返回登录页面。我想我一定是错过了什么小东西。下面是完整的代码减去实际用户/pwd。我希望有人知道出了什么问题:)


您可能需要使用
HttpUtility.UrlEncode(string)
来发布特殊字符,如
!@$%^&*()_+?:;“{[}]\


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

当您执行web请求时,它与您通过浏览器访问的内容是分开的。因此,如果您使用web请求登录,它仅在web请求内有效。当您通过浏览器发出请求时,您仍然需要登录。reference f或者HTTP实用程序是System.Web
$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();
        }
    }
}