0

I edited this for better readability. This is also a different question

The purpose of this code is to convert a CookieContainer object to a HashTable for later conversion to a CookieCollection. I get the error "Cannot find variable 'm_domainTable'" for this code

var cookieJar = new CookieContainer();
cookieJar.Add(new Uri("http://someuri.com"), new Cookie("name", "value", "/path/", ".domain"));
var table = (Hashtable) cookieJar.GetType().InvokeMember("m_domainTable",
            BindingFlags.NonPublic |
            BindingFlags.GetField |
            BindingFlags.Instance,
            null,
            cookieJar,
            new object[] {});

The purpose of even using this hack is to be able to cycle through a locally stored cookie file. I tried creating a new project using this code suggested below but I get the error "Object not set to an instance of an Object".

var cookieJar = new CookieContainer();
var table = (Hashtable)cookieJar.GetType()
        .GetField("m_domainTable", BindingFlags.NonPublic | BindingFlags.Instance)
        .GetValue(cookieJar);

So I am stuck. Previously I had shown my SendPost() method but since this error only involves these bits of code I took out that method. If you need any more code or need me to test anything let me know as I would love to get this working.

My enviornment setup is MonoDevelop using Mono / .Net 4.5 using these assemblies:

using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections;
using System.Web;
Community
  • 1
  • 1
gh0st
  • 33
  • 1
  • 12
  • Have you tried setting a [User-Agent](https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.useragent.aspx) for the request? [List of UA strings per browser](http://www.useragentstring.com/pages/Browserlist/) – DGibbs Apr 09 '15 at 08:39
  • Yeah, I actually didn't include a UA in the code here but I have one in my local code. I'll update that, sorry for not including it. – gh0st Apr 09 '15 at 08:43

1 Answers1

1

Well, you can access this private field like

var table = (Hashtable)cookieJar.GetType()
                .GetField("m_domainTable", BindingFlags.NonPublic | BindingFlags.Instance)
                .GetValue(cookieJar);

Update

I missed mono tag in your original question. But since your working environment is Mono, not Microsoft CLR - then you might looking in the wrong way.

As far as I can see - m_domainTable hashtable exists only in Microsoft implementation, but not in Mono.

It can be proved by simple test code:

var cookieJar = new CookieContainer();
var type = cookieJar.GetType();
var fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);

foreach (var field in fields)
    Console.WriteLine(field.ToString());

Then being run in Microsoft CLR, it outputs:

System.Collections.Hashtable m_domainTable
Int32 m_maxCookieSize
Int32 m_maxCookies
Int32 m_maxCookiesPerDomain
Int32 m_count
System.String m_fqdnMyDomain

but if you'll run it in Ideone (I suppose it is running Mono there), you'll get:

System.Int32 capacity
System.Int32 perDomainCapacity
System.Int32 maxCookieSize
System.Net.CookieCollection cookies

And have a look here: System.Net.CookieCollection cookies - it seems here is the field you need....

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
  • Updated OP and the code you gave effectively called "m_domainTable" but gave me a NullReferenceException for whatever reason that I don't understand. – gh0st Apr 09 '15 at 09:13
  • What exactly part of my code throws NullReferenceException to you? In fact code snippet `var cookieJar = new CookieContainer();` + the rest of code I've posted works for me and doesn't throws exception. – Andrey Korneyev Apr 09 '15 at 09:17
  • Updated my code. I still get a nullreferenceexception at the last line of your code snippet: .GetValue(cookieJar); – gh0st Apr 09 '15 at 09:46
  • @ghost_53574 well.... I don't know what's happening on your side. Code snippet copied from your last update works on my side, so since I can't reproduce your problem - it looks like I can't help you. – Andrey Korneyev Apr 09 '15 at 09:59
  • Well like I said, I made a new project, put that code I referenced above in, and it blew up in my face... Is there any other way to get cookies from a CookieContainer besides GetCookies()? – gh0st Apr 09 '15 at 10:04
  • Updated my question if you would like to take a look. I used Ideone to try and compile your code but it still fails https://ideone.com/17gCna – gh0st Apr 10 '15 at 05:04