-8

I have a C# program and try to read the cookies with a static method:

[DllImport("wininet.dll", SetLastError = true)]
        public static extern bool InternetGetCookieEx(
        string url,
        string cookieName,
        StringBuilder cookieData,
        ref int size,
        Int32 dwFlags,
        IntPtr lpReserved);

        private const Int32 InternetCookieHttponly = 0x2000;

        /// <summary>
        /// Gets the URI cookie container.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <returns></returns>
        public static CookieContainer GetUriCookieContainer(Uri uri)
        {
            CookieContainer cookies = null;
            // Determine the size of the cookie
            int datasize = 8192 * 16;
            StringBuilder cookieData = new StringBuilder(datasize);
            if (!InternetGetCookieEx(uri.ToString(), null, cookieData, ref datasize, InternetCookieHttponly, IntPtr.Zero))
            {
                if (datasize < 0)
                    return null;
                // Allocate stringbuilder large enough to hold the cookie
                cookieData = new StringBuilder(datasize);
                if (!InternetGetCookieEx(
                    uri.ToString(),
                    null, cookieData,
                    ref datasize,
                    InternetCookieHttponly,
                    IntPtr.Zero))
                    return null;
            }
            if (cookieData.Length > 0)
            {
                cookies = new CookieContainer();
                cookies.SetCookies(uri, cookieData.ToString().Replace(';', ','));
            }
            return cookies ?? new CookieContainer();
        }

The code is from this answer.

So if the cookies is null I am returning a new CookieContainer which cannot be null.

But when I am calling this method in my application the return value is null.

var cookie = Util.GetUriCookieContainer(uri);

if (cookie == null)
 {
    MessageBox.Show("cookie is null");
 }

Am I missing something?

Community
  • 1
  • 1
jihn
  • 51
  • 4
  • 5
    I see two `return null;` lines in that method. If you think it can *never* be `null`, what are those lines for? – David May 20 '16 at 18:29
  • 3
    Stepping though with a debugger would have easily found out what the problem was. You should try to do that before asking a question here (that is likely why you are getting so many downvotes). – Scott Chamberlain May 20 '16 at 18:30
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – tnw May 20 '16 at 18:34

1 Answers1

4

Cannot be null ? What is that ?

if (!InternetGetCookieEx(uri.ToString(), null, cookieData, ref datasize, InternetCookieHttponly, IntPtr.Zero))
{
    if (datasize < 0)
        return null;
    // Allocate stringbuilder large enough to hold the cookie
    cookieData = new StringBuilder(datasize);
    if (!InternetGetCookieEx(
            uri.ToString(),
            null, cookieData,
            ref datasize,
            InternetCookieHttponly,
            IntPtr.Zero))
        return null;
}
romain-aga
  • 1,411
  • 7
  • 14