“未启用Cookie”HttpWebResponse C#

时间:2013-01-28 22:00:52

标签: c# cookies

我正在尝试通过Windows应用程序发布数据,但最后我不断收到回复:“您必须启用Cookie才能访问成员区域!”。

我不知道我的代码中缺少什么。谢谢你的帮助。 :)

    public void Login()
    {
        string url = @"http://freedns.afraid.org/zc.php?step=2";
        //WebClient webclient = new WebClient();

        byte[] postValues = Encoding.ASCII.GetBytes("username=username&password=password&action=auth&remember=1&submit=Login");

        //get cookies
        CookieCollection cookies = new CookieCollection();
        HttpWebRequest askForCookies = (HttpWebRequest)WebRequest.Create("http://freedns.afraid.org/subdomain/");
        askForCookies.CookieContainer = new CookieContainer();
        askForCookies.CookieContainer.Add(cookies);
        HttpWebResponse responseCookies = (HttpWebResponse)askForCookies.GetResponse();
        cookies = responseCookies.Cookies;
        //Cookies retrived
        int howmuch = responseCookies.Cookies.Count;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.CookieContainer = new CookieContainer();
        request.CookieContainer.Add(cookies);
        request.Credentials = CredentialCache.DefaultCredentials;
        request.UserAgent = "mateuszzaborowski.com update client";
        request.Method = WebRequestMethods.Http.Post;

        request.ContentType = "application/x-www-form-urlencoded";
        Stream postStream = request.GetRequestStream();

        postStream.Write(postValues, 0, postValues.Length);
        postStream.Close();

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream Anwser = response.GetResponseStream();
        StreamReader _Anwser = new StreamReader(Anwser);
        string test = _Anwser.ReadToEnd();
        _Anwser.Close();
    }

0 个答案:

没有答案