Beispiel #1
0
        private static uint TryGetKey(string cookie, string sessionName)
        {
            string url = String.Format("http://secure.formula1.com/reg/getkey/{0}.asp?auth={1}", sessionName, cookie);

#if WINDOWS_PHONE || WINRT
            HttpWebAdaptor req = new HttpWebAdaptor(WebRequest.Create(url) as HttpWebRequest);
#else
            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;

            if (null != req.Proxy)
            {
                req.Proxy.Credentials = CredentialCache.DefaultCredentials;
            }
#endif

            HttpWebResponse resp1 = req.GetResponse() as HttpWebResponse;

            if (null != resp1)
            {
                using (TextReader re = new StreamReader(resp1.GetResponseStream()))
                {
                    return(ParseKey(re.ReadLine()));
                }
            }

            return(INVALID_KEY);
        }
Beispiel #2
0
        private static Stream GetKeyFrame(string url)
        {
#if WINDOWS_PHONE || WINRT
            HttpWebAdaptor req = new HttpWebAdaptor(WebRequest.Create(url) as HttpWebRequest);
#else
            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
#endif

            if (null == req)
            {
                throw new KeyFrameException("Could not create request for url: " + url, null);
            }

#if !WINDOWS_PHONE && !WINRT
            if (null != req.Proxy)
            {
                req.Proxy.Credentials = CredentialCache.DefaultCredentials;
            }
#endif

            HttpWebResponse resp1 = req.GetResponse() as HttpWebResponse;

            if (null == resp1)
            {
                throw new KeyFrameException("Error retrieving KeyFrame", null);
            }

            MemoryStream ms = new MemoryStream(ESTIMATED_SIZE);
            using (Stream s = resp1.GetResponseStream())
            {
                int    read;
                byte[] next = new byte[READ_CHUNK_SIZE];
                do
                {
                    read = s.Read(next, 0, READ_CHUNK_SIZE);
                    if (read > 0)
                    {
                        ms.Write(next, 0, read);
                    }
                } while (read > 0);
            }

            ms.Seek(0, SeekOrigin.Begin);
            return(ms);
        }
Beispiel #3
0
        private static string TryGetCookie(string user, string pass, string baseurl)
        {
            string body = string.Format("email={0}&password={1}", user, pass);

            byte[] bodyData = StringUtils.StringToASCIIBytes(body);

#if WINRT
            HttpClientHandler handler = new HttpClientHandler();
            handler.AllowAutoRedirect = false;
            handler.UseCookies        = true;
            handler.CookieContainer   = new CookieContainer();

            HttpClient         client  = new HttpClient(handler);
            HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, new Uri(baseurl));
            message.Content = new FormUrlEncodedContent(
                new Dictionary <string, string>()
            {
                { "email", user }, { "password", pass }
            }
                );

            HttpResponseMessage response = client.SendAsync(message).Result;
            if (response.Headers.Contains("Set-Cookie"))
            {
                foreach (var cookie in response.Headers.GetValues("Set-Cookie"))
                {
                    if (!string.IsNullOrEmpty(cookie))
                    {
                        return(ParseCookie(cookie));
                    }
                }
            }
#else
    #if WINDOWS_PHONE
            HttpWebAdaptor req = new HttpWebAdaptor(WebRequest.Create(baseurl) as HttpWebRequest);

            req.Request.AllowAutoRedirect = false;
            //req.Request.CookieContainer = new CookieContainer();
            req.Request.Method      = "POST";
            req.Request.ContentType = "application/x-www-form-urlencoded";
            req.Request.Headers[HttpRequestHeader.ContentLength] = bodyData.Length.ToString();
    #else
            HttpWebRequest req = WebRequest.Create(baseurl) as HttpWebRequest;

            if (null != req.Proxy)
            {
                req.Proxy.Credentials = CredentialCache.DefaultCredentials;
            }

            req.AllowAutoRedirect = false;
            req.Method            = "Post";
            req.ContentType       = "application/x-www-form-urlencoded";
            req.ContentLength     = bodyData.Length;
    #endif
            using (Stream reqBody = req.GetRequestStream())
            {
                reqBody.Write(bodyData, 0, bodyData.Length);
                reqBody.Flush();
                reqBody.Close();
            }

            HttpWebResponse resp1 = req.GetResponse() as HttpWebResponse;

            if (null != resp1)
            {
                string cookie = resp1.Headers["Set-Cookie"];

                if (string.IsNullOrEmpty(cookie))
                {
                    //if (0 < resp1.ContentLength)
                    //{
                    //    // it's probably not an event day, and the server is returning a singlecharacter
                    //    StreamReader stringReader = new StreamReader(resp1.GetResponseStream());

                    //    return stringReader.ReadToEnd();
                    //}

                    return(null);
                }

                return(ParseCookie(cookie));
            }
#endif

            return("");
        }