コード例 #1
0
        public SendResult send(string link, DailyQueueConfiguration config)
        {
            if (config.User == null || config.Password == null)
            {
                return SendResult.NOT_CONFIGURED;
            }

            bool trying = true;
            CookieContainer cookieJar = new CookieContainer();

            while (trying)
            {

                //the request
                string sRequest = Constants.getDailyQueueAddRequest();
                string content = "{ url : '" + link + "', authenticity_token : '" + config.AuthenticityToken + "' }";
                byte[] contentBytes = Encoding.ASCII.GetBytes(content);

                // prepare the web page we will be asking for
                HttpWebRequest request = createHttpRequest(sRequest, "POST", "application/json", contentBytes, cookieJar, config);

                // execute the request
                HttpWebResponse response;
                try
                {
                    response = (HttpWebResponse)request.GetResponse();

                }
                catch (WebException e)
                {
                    string message = e.Message;
                    return SendResult.FAILURE;
                }

                string json = parseHttpResponse(response);
                JObject o = JObject.Parse(json);
                JObject oLink = (JObject)o["link"];
                if (oLink != null)
                {
                    if (oLink["id"].ToString() != "")
                    {
                        return SendResult.SUCCESS;
                    }
                    else
                    {
                        return SendResult.FAILURE;
                    }
                }
                else
                {
                    if (authenticate(ref config, cookieJar) == SendResult.FAILURE)
                    {
                        trying = false;
                    }
                }
            }

            return SendResult.FAILURE;
        }
コード例 #2
0
        private SendResult authenticate(ref DailyQueueConfiguration config, CookieContainer cookieJar)
        {
            //the request
            string sRequest = Constants.getDailyQueueSignInRequest();
            string content = "{remote: true, commit: 'Sign in', utf8: '✓', user: {remember_me: 1, password: '******', email: '" + config.User + "'}}";
            byte[] contentBytes = Encoding.ASCII.GetBytes(content);

            // prepare the web page we will be asking for
            HttpWebRequest request = createHttpRequest(sRequest, "POST", "application/json", contentBytes, cookieJar, config);

            // execute the request
            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
                config.Cookies = cookieJar.GetCookies(request.RequestUri);
            }
            catch (WebException e)
            {
                string message = e.Message;
                return SendResult.FAILURE;
            }

            string json = parseHttpResponse(response);
            try
            {
                JObject o = JObject.Parse(json);
                JValue oSuccess = (JValue)o["success"];
                JValue oAuthenticityToken = (JValue)o["authenticity_token"];
                if ((bool)oSuccess.Value)
                {
                    config.AuthenticityToken = oAuthenticityToken.Value.ToString();
                    return SendResult.SUCCESS;
                }
                else
                {
                    return SendResult.FAILURE;
                }
            }
            catch (Exception e)
            {
                string message = e.Message;
                return SendResult.FAILURE;
            }
        }
コード例 #3
0
 public DailyQueueForm()
 {
     InitializeComponent();
     config = new DailyQueueConfiguration();
     ghk = new HotKeys.GlobalHotkey(HotKeys.Constants.CTRL + HotKeys.Constants.SHIFT, Keys.C, this);
     notifyIcon = new NotifyIcon();
     MenuItem[] menuItems = new MenuItem[2];
     MenuItem configureItem = new MenuItem();
     configureItem.Text = "configure";
     configureItem.Click += new System.EventHandler(this.configureItem_Click);
     menuItems[0] = configureItem;
     MenuItem closeItem = new MenuItem();
     closeItem.Text = "quit";
     closeItem.Click += new System.EventHandler(this.closeItem_Click);
     menuItems[1] = closeItem;
     contextMenu = new ContextMenu(menuItems);
     notifyIcon.Icon = new Icon("appicon.ico");
     notifyIcon.ContextMenu = contextMenu;
     notifyIcon.Text = "DailyQueue";
     notifyIcon.Visible = true;
 }
コード例 #4
0
 private HttpWebRequest createHttpRequest(string sRequest, string method, string contentType, byte[] contentBytes, CookieContainer cookieJar, DailyQueueConfiguration config)
 {
     HttpWebRequest request = (HttpWebRequest)
         WebRequest.Create(sRequest);
     request.Method = method;
     cookieJar.Add(config.Cookies);
     request.CookieContainer = cookieJar;
     request.ContentLength = contentBytes.Length;
     request.ContentType = contentType;
     if (contentBytes != null)
     {
         Stream requestStream = request.GetRequestStream();
         requestStream.Write(contentBytes, 0, contentBytes.Length);
         requestStream.Close();
     }
     return request;
 }