开发者

CookieContainer doesn't save all cookies

I'm using WebClient to login to Wordpress blog, my Web Client methods:

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

Now when I try to login wordpress send four cookies and 302 redirect

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

but WebResponse contains only 2 cookies (wth path=/) and when it automaticaly redirects it doesn't send 2 another cookies so I can't log in. Now I fixed this by handling redirects and cookies manualy from response.Headers["Set-Cookie"] and response.Headers["Location"] . But I wonder is there any other solution?

Actual fix

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

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

         开发者_如何学C           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);
                }


Try this:

request.AllowAutoRedirect = false; 


What you want to do is:

  • Find out the 'Post' that gets sent to the wordpress site when you log in.
    • The 'Live HTTP Headers' addon for firefox is a good one for checking out headers.
  • Check the Post info.
  • Then recreate sending that info using an HttpWebRequest.
    • It's a good idea to add a real UserAgent string to the request so you look like a real person
    • Not as important, but having a logical referer on the request isn't a bad idea either
  • Attach your CookieContainer to the request to grab the login cookies.
  • In order to not have to do this on every launch of your program:
    • You can serialize and deserialize your CookieContainer
    • For serialization I've personally successfully used BinaryFormatter

I'm not sure if providing code examples would be constructive or not. With this info, finding out how to code it yourself is more rewarding than just copying and pasting. However if you would like code examples for any of it just leave a comment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜