locked
CookieContainer in .net3.5 for windows mobile 6 RRS feed

  • Question

  • Hi all,

    I am fairly new to programming for windows mobile and I cannot find a solution to this problem.
    I am building an application which posts data to a website, the problem is that the website issues a cookie that I need to use. I found that usally CookieContainer can be used for this, but this is not available in .net3.5 for windows mobile. Does anyone know what I can use instead? There must be something that replaced CookieContainer?

    any help is greatly appreciated

    Many thnx in advance

    (p.s. The application i am developing is in C# using visual basic 2008)
    • Moved by eryang Thursday, November 5, 2009 7:51 AM wrong forum (From:.NET Base Class Library)
    Tuesday, November 3, 2009 8:46 PM

Answers

  • CookieContainers are not implemented in the .Net compact framework.  You will need to manage them yourself.  Here is one solution that some one wrote.

    http://circumdev.net/post/Net-CF-HttpWebRequest-and-HttpWebResponse-Have-No-Cookie-Support.aspx


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    
    namespace net.frejos.http
    {
        public class CookieManager
        {
            private Dictionary cookieValues;
    
            public Dictionary CookieValues {
                get {
                    if (this.cookieValues == null) {
                        this.cookieValues = new Dictionary();
                    }
    
                    return this.cookieValues;
                }
            }
    
            public void PublishCookies(HttpWebRequest webRequest)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("Cookie: ");
                foreach (string key in this.CookieValues.Keys) {
                    sb.Append(key);
                    sb.Append("=");
                    sb.Append(this.CookieValues[key]);
                    sb.Append("; ");
                    sb.Append("$Path=\"/\"; ");               
                }
    
                webRequest.Headers.Add(sb.ToString());
                sb = null;
                webRequest = null;
            }
    
            public void StoreCookies(HttpWebResponse webResponse)
            {
                for (int x=0; x < webResponse.Headers.Count; x++) {
                    if (webResponse.Headers.Keys[x].ToLower().Equals("set-cookie")) {
                        this.AddRawCookie( webResponse.Headers[x] );
                    }
                }
    
                webResponse = null;
            }
    
            private void AddRawCookie(string rawCookieData)
            {
                string key = null;
                string value = null;
    
                string[] entries = null;
    
                if (rawCookieData.IndexOf(",") > 0)
                {
                    entries = rawCookieData.Split(',');
                }
                else { 
                    entries = new string[] { rawCookieData };
                }
    
                foreach (string entry in entries) {
                    string cookieData = entry.Trim();
    
                    if (cookieData.IndexOf(';') > 0)
                    {
                        string[] temp = cookieData.Split(';');
                        cookieData = temp[0];
                    }
    
                    int index = cookieData.IndexOf('=');
                    if (index > 0)
                    {
                        key = cookieData.Substring(0, index);
                        value = cookieData.Substring(index + 1);
                    }
    
                    if (key != null && value != null)
                    {
                        this.CookieValues[key] = value;
                    }
    
                    cookieData = null;
                }
    
                rawCookieData = null;
                entries = null;
                key = null;
                value = null;
            }
    
            public override string ToString()
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("[");
                foreach (string key in this.CookieValues.Keys) {
                    sb.Append("{");
                    sb.Append(key);
                    sb.Append(",");
                    sb.Append(this.CookieValues[key]);
                    sb.Append("}, ");
                }
                if (this.CookieValues.Keys.Count > 0)
                {
                    sb.Remove(sb.Length - 2, 2);
                }
                sb.Append("]");
    
                return sb.ToString();
            }
        }
    }
    

     



    Here is a usage example:

    CookieManager cookieManager = new CookieManager();
    // Set a cookie value
    cookieManager.CookieValues["FavoriteCookie"] = "Chocolate Chip";
    
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    // Publish the cookies to the request before asking for the response
    cookieManager.PublishCookies(webRequest);
    
    HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
    // Store any cookies returned from the response
    cookieManager.StoreCookies(webResponse);
    
    // Get the value of a cookie
    string session = cookieManager.CookieValues["SESSIONID"];
    
    webRequest = (HttpWebRequest)WebRequest.Create(url2);
    cookieManager.PublishCookies(webRequest);
    
    webResponse = (HttpWebResponse)webRequest.GetResponse();
    

    Joel Ivory Johnson
    It takes all the running you can do to stay in one place.If you want to get somewhere else,you must try to run at least twice as fast as that.
    • Proposed as answer by Joel Ivory Johnson Thursday, November 5, 2009 10:29 AM
    • Marked as answer by warrentang Monday, November 9, 2009 4:06 AM
    Thursday, November 5, 2009 10:29 AM