/// <exception cref="System.Exception"></exception>
        public virtual void TestEncodeDecodeCookie()
        {
            PersistentCookieStore cookieStore = new PersistentCookieStore(database);
            string cookieName   = "foo";
            string cookieVal    = "bar";
            bool   isSecure     = false;
            bool   httpOnly     = false;
            string cookiePath   = "baz";
            string cookieDomain = "foo.com";
            // expiration date - 1 day from now
            Calendar cal = Calendar.GetInstance();

            cal.SetTime(new DateTime());
            int numDaysToAdd = 1;

            cal.Add(Calendar.Date, numDaysToAdd);
            DateTime          expirationDate = cal.GetTime();
            BasicClientCookie cookie         = new BasicClientCookie(cookieName, cookieVal);

            cookie.SetExpiryDate(expirationDate);
            cookie.SetSecure(isSecure);
            cookie.SetDomain(cookieDomain);
            cookie.SetPath(cookiePath);
            string encodedCookie = cookieStore.EncodeCookie(new SerializableCookie(cookie));

            Apache.Http.Cookie.Cookie fetchedCookie = cookieStore.DecodeCookie(encodedCookie);
            NUnit.Framework.Assert.AreEqual(cookieName, fetchedCookie.GetName());
            NUnit.Framework.Assert.AreEqual(cookieVal, fetchedCookie.GetValue());
            NUnit.Framework.Assert.AreEqual(expirationDate, fetchedCookie.GetExpiryDate());
            NUnit.Framework.Assert.AreEqual(cookiePath, fetchedCookie.GetPath());
            NUnit.Framework.Assert.AreEqual(cookieDomain, fetchedCookie.GetDomain());
        }
 /// <exception cref="System.Exception"></exception>
 public virtual void TestEncodeDecodeCookie()
 {
     PersistentCookieStore cookieStore = new PersistentCookieStore(database);
     string cookieName = "foo";
     string cookieVal = "bar";
     bool isSecure = false;
     bool httpOnly = false;
     string cookiePath = "baz";
     string cookieDomain = "foo.com";
     // expiration date - 1 day from now
     Calendar cal = Calendar.GetInstance();
     cal.SetTime(new DateTime());
     int numDaysToAdd = 1;
     cal.Add(Calendar.Date, numDaysToAdd);
     DateTime expirationDate = cal.GetTime();
     BasicClientCookie cookie = new BasicClientCookie(cookieName, cookieVal);
     cookie.SetExpiryDate(expirationDate);
     cookie.SetSecure(isSecure);
     cookie.SetDomain(cookieDomain);
     cookie.SetPath(cookiePath);
     string encodedCookie = cookieStore.EncodeCookie(new SerializableCookie(cookie));
     Apache.Http.Cookie.Cookie fetchedCookie = cookieStore.DecodeCookie(encodedCookie);
     NUnit.Framework.Assert.AreEqual(cookieName, fetchedCookie.GetName());
     NUnit.Framework.Assert.AreEqual(cookieVal, fetchedCookie.GetValue());
     NUnit.Framework.Assert.AreEqual(expirationDate, fetchedCookie.GetExpiryDate());
     NUnit.Framework.Assert.AreEqual(cookiePath, fetchedCookie.GetPath());
     NUnit.Framework.Assert.AreEqual(cookieDomain, fetchedCookie.GetDomain());
 }
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="System.TypeLoadException"></exception>
        private void ReadObject(ObjectInputStream @in)
        {
            string name  = (string)@in.ReadObject();
            string value = (string)@in.ReadObject();

            clientCookie = new BasicClientCookie(name, value);
            clientCookie.SetComment((string)@in.ReadObject());
            clientCookie.SetDomain((string)@in.ReadObject());
            clientCookie.SetExpiryDate((DateTime)@in.ReadObject());
            clientCookie.SetPath((string)@in.ReadObject());
            clientCookie.SetVersion(@in.ReadInt());
            clientCookie.SetSecure(@in.ReadBoolean());
        }
Beispiel #4
0
        public virtual string httpGet(string url)
        {
            string domain = Resources.getString([email protected]_domain);
            string result = "";

            CookieStore      cookieStore  = new BasicCookieStore();
            BasicHttpContext localContext = new BasicHttpContext();

            localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

            // If we have a cookie stored, parse and use it. Otherwise, use a default http client.
            try
            {
                HttpClient httpClient = new DefaultHttpClient();
                HttpGet    httpGet    = new HttpGet(url);
                if (!authorizationCookie.Equals(""))
                {
                    string[] cookies = authorizationCookie.Split(";", true);
                    for (int i = 0; i < cookies.Length; i++)
                    {
                        string[] kvp = cookies[i].Split("=", true);
                        if (kvp.Length != 2)
                        {
                            throw new Exception("Illegal cookie: missing key/value pair.");
                        }
                        BasicClientCookie c = new BasicClientCookie(kvp[0], kvp[1]);
                        c.Domain = domain;
                        cookieStore.addCookie(c);
                    }
                }
                HttpResponse httpResponse = httpClient.execute(httpGet, localContext);
                result = EntityUtils.ToString(httpResponse.Entity);
            }
            catch (Exception e)
            {
                Log.e(TAG, e.LocalizedMessage);
            }

            return(result);
        }
 /// <exception cref="System.IO.IOException"></exception>
 /// <exception cref="System.TypeLoadException"></exception>
 private void ReadObject(ObjectInputStream @in)
 {
     string name = (string)@in.ReadObject();
     string value = (string)@in.ReadObject();
     clientCookie = new BasicClientCookie(name, value);
     clientCookie.SetComment((string)@in.ReadObject());
     clientCookie.SetDomain((string)@in.ReadObject());
     clientCookie.SetExpiryDate((DateTime)@in.ReadObject());
     clientCookie.SetPath((string)@in.ReadObject());
     clientCookie.SetVersion(@in.ReadInt());
     clientCookie.SetSecure(@in.ReadBoolean());
 }