Пример #1
0
        public void InitClient(CookieContainer cookieContainer)
        {
            if (client != null)
            {
                client.Dispose();
            }

            if (Device.RuntimePlatform == Device.Android ||
                Device.RuntimePlatform == Device.iOS)
            {
                var cookies = cookieContainer.List();
                cookieHandler = new NativeCookieHandler();
                cookieHandler.SetCookies(cookieContainer.List());

                handler = new NativeMessageHandler(false, false, cookieHandler)
                {
                    AllowAutoRedirect      = true,
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
                };
            }
            else
            {
                handler = new HttpClientHandler
                {
                    CookieContainer        = cookieContainer,
                    AllowAutoRedirect      = true,
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
                };
            }

            client = new HttpClient(handler)
            {
                Timeout = TimeSpan.FromSeconds(timeout_in_seconds),
                DefaultRequestHeaders =
                {
                    CacheControl = CacheControlHeaderValue.Parse("no-cache, must-revalidate"),
                }
            };

            client.DefaultRequestHeaders.UserAgent.ParseAdd("Tatoeba.Mobile/1.1");
        }
Пример #2
0
 public static CookieContainer FixCookies(this CookieContainer collection)
 {
     foreach (Cookie cookie in collection.List())
     {
         if (cookie.Version == 1)
         {
             cookie.Version = 0;
         }
         if (!string.IsNullOrEmpty(cookie.Path))
         {
             int length = cookie.Path.LastIndexOf('/');
             if (length != -1)
             {
                 cookie.Path = cookie.Path.Substring(0, length);
             }
         }
     }
     return(collection);
 }
Пример #3
0
        public override bool Login()
        {
            CookieContainer cookieJar = new CookieContainer();

            _client         = new CookieAwareWebClient(cookieJar);
            _client.Referer = HOME_URL;

            // the website sets some cookie that is needed for login
            string response = _client.DownloadString(HOME_URL);

            Cookie tokenCookie = cookieJar.List().FirstOrDefault(c => c.Name == "csrftoken");

            StringBuilder postData = new StringBuilder();

            postData.Append("email=" + HttpUtility.UrlEncode(Username) + "&");
            postData.Append("password="******"&");
            postData.Append("remember=False=");

            // the csrf token is sent in the hader
            _client.Headers.Add("User-Agent", USER_AGENT);
            _client.Headers.Add("Accept", "application/json, text/javascript, */*; q=0.01");
            _client.Headers.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
            _client.Headers.Add("Referer", HOME_URL);
            _client.Headers.Add("X-Requested-With", "XMLHttpRequest");
            _client.Headers.Add("X-CSRFToken", tokenCookie.Value);

            response = _client.UploadString(LOGIN_API, postData.ToString());
            JObject jObject = JObject.Parse(response);
            JToken  jToken  = jObject.GetValue("success");

            if (!jToken.Value <bool>())
            {
                //The query returned false, either not authenticated or forbiddent (403)
                Console.WriteLine("Wrong email or password logging into Edx.");
                return(false);
            }

            //Now get the goods (cookies should be set!)

            return(true);
        }
Пример #4
0
 public static Cookie GetCookieByName(this CookieContainer container, string name)
 {
     return(container.List().FirstOrDefault(c => c.Name == name));
 }