c# - CookieContainer 不保存所有 cookie

标签 c#

我正在使用 WebClient 登录 Wordpress 博客,我的 Web 客户端方法:

protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);

        if (request.GetType() == typeof(HttpWebRequest))
        {
            ((HttpWebRequest)request).CookieContainer = cookieContainer;
            foreach (Cookie c in cookieContainer.GetCookies(address))
            {
                Console.WriteLine(" cName:   " + c.Name);
                Console.WriteLine(" cValue:  " + c.Value);
                Console.WriteLine(" cPath:   " + c.Path);
                Console.WriteLine(" cDomain: " + c.Domain);
            }
            ((HttpWebRequest)request).UserAgent = userAgent;
            ((HttpWebRequest)request).Timeout = timeout;
            ((HttpWebRequest)request).Accept = accept;
        }

        return request;
    }

    [DebuggerNonUserCode()]
    protected override WebResponse GetWebResponse(WebRequest request)
    {
        try
        {
            WebResponse response = base.GetWebResponse(request);

            if (response.GetType() == typeof(HttpWebResponse))
            {
                foreach (Cookie c in ((HttpWebResponse)response).Cookies)
                {
                    Console.WriteLine(" Name:   " + c.Name);
                    Console.WriteLine(" Value:  " + c.Value);
                    Console.WriteLine(" Path:   " + c.Path);
                    Console.WriteLine(" Domain: " + c.Domain);
                }

                characterSet = ((HttpWebResponse)response).CharacterSet;
            }

            return response;
        }
        catch (System.Net.WebException e)
        {
            throw e;
        }
    }

现在当我尝试登录 wordpress 时发送四个 cookie 和 302 重定向

Set-Cookie: wordpress_a235c74829f55a618a01c9f088805f08=precmast%7C1318622660%7C45c78363a5062b592b1fe49201fea5a8; path=/wp-content/plugins; httponly
Set-Cookie: wordpress_test_cookie=WP+Cookie+check; path=/
Set-Cookie: wordpress_a235c74829f55a618a01c9f088805f08=precmast%7C1318622660%7C45c78363a5062b592b1fe49201fea5a8; path=/wp-admin; httponly
Set-Cookie: wordpress_logged_in_a235c74829f55a618a01c9f088805f08=precmast%7C1318622558%7Ca28c4bf14832cbbee606cdddcad9e019; path=/; httponly

但 WebResponse 仅包含 2 个 cookie(wth path=/),当它自动重定向时,它不会发送另外 2 个 cookie,因此我无法登录。现在我通过手动处理 response.Headers 中的重定向和 cookie 来修复此问题["Set-Cookie"] 和 response.Headers["Location"] 。但是我想知道还有其他解决方案吗?

实际修复

 if (response.GetType() == typeof(HttpWebResponse))
            {
                if(response.Headers["Location"] != null)
                {

                    cookieContainer.Add(response.ResponseUri, GetAllCookiesFromHeader(response.Headers["Set-Cookie"], response.ResponseUri.Host));

                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(response.Headers["Location"]);
                    req.Method = "GET";
                    req.AllowAutoRedirect = false;
                    req.CookieContainer = cookieContainer;
                    req.UserAgent = userAgent;
                    req.Timeout = timeout;
                    req.Accept = accept;


                    return GetWebResponse(req);
                }

最佳答案

试试这个:

request.AllowAutoRedirect = false; 

关于c# - CookieContainer 不保存所有 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7746204/

相关文章:

c# - EmguCV 类型初始化异常

c# - 如何修复 GattStatus : 3 - WriteNotPermitted exception for BLE Xamarin forms application?

c# - 在类上调用静态方法时出现 TypeInitializationException 的原因可能是什么?

c# - 如何在 WPF/WinForms 中仅关闭 ESC 键上的单个对话框?

c# - 如何将值设置为图标?

c# - 具有大字体的多行复选框的左上框对齐

c# - 写入日志文件类

c# - 流畅的 nHibernate 配置

c# - 将鼠标悬停在通知图标上显示文本

c# - 什么是循环依赖,我该如何解决?