cookicontainer不会保存所有的cookie

本文关键字:cookie 保存 cookicontainer | 更新日期: 2023-09-27 18:11:01

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

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 (path=/),当它自动重定向时,它不会发送另外2个cookie,所以我无法登录。现在我通过手动处理响应中的重定向和cookie解决了这个问题。报头["Set-Cookie"]和响应。标题("位置")。但我想知道有没有其他的解决办法?

实际解决

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

cookicontainer不会保存所有的cookie

试试这个:

request.AllowAutoRedirect = false; 

你要做的是:

  • 找出当你登录时发送到wordpress网站的"Post"。
    • firefox的'Live HTTP Headers'插件是一个检查报头的好插件。
  • 查看Post信息
  • 然后使用HttpWebRequest重新创建发送该信息。
    • 在请求中添加一个真实的UserAgent字符串是个好主意,这样你看起来就像一个真实的人
    • 不那么重要,但是在请求上有一个逻辑引用者也不是一个坏主意
  • 将CookieContainer附加到请求以获取登录cookie。
  • 为了避免在每次启动程序时都这样做:
    • 可以序列化和反序列化CookieContainer
    • 对于序列化,我个人成功地使用了BinaryFormatter

我不确定提供代码示例是否具有建设性。有了这些信息,找出如何自己编写代码比仅仅复制和粘贴更有意义。但是,如果你想要任何代码示例,请留下评论