Exemple #1
0
        /// <summary>
        /// Get the specified resource contents
        /// </summary>
        public void Post <T>(String resourcePath, T data)
        {
            int retry = 0;

            while (retry++ < 3)
            {
                try
                {
                    Uri        requestUri = new Uri(String.Format("{0}/{1}", this.m_baseUri, resourcePath));
                    WebRequest request    = WebRequest.Create(requestUri);
                    request.Method      = "POST";
                    request.ContentType = "application/json";

                    if (!String.IsNullOrEmpty(s_username))
                    {
                        request.Headers.Add("Authorization", String.Format("Basic {0}", Convert.ToBase64String(Encoding.UTF8.GetBytes(String.Format("{0}:{1}", s_username, s_password)))));
                    }

                    var serializer = new DataContractJsonSerializer(typeof(T));
                    serializer.WriteObject(request.GetRequestStream(), data);
                    var response = request.GetResponse();
                    return;
                }
                catch (WebException e)
                {
                    if ((e.Response as HttpWebResponse).StatusCode == HttpStatusCode.Unauthorized)
                    {
                        frmAuthenticate authForm = new frmAuthenticate();
                        if (authForm.ShowDialog() == DialogResult.OK)
                        {
                            s_username = authForm.Username;
                            s_password = authForm.Password;
                        }
                        else
                        {
                            throw new SecurityException("Authorization for service failed!");
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
                catch (Exception e)
                {
                    throw;
                }
            }
            throw new SecurityException("Authorization for service failed!");
        }
Exemple #2
0
        void Login(FormStartPosition p)
        {
            Util.XmppServices.XmppCon = new XmppClientConnection();
            CreateEvents();
            using (frmAuthenticate frm = new frmAuthenticate(rosterControl))
            {
                frm.OnLog += new OnLogHandler(frm_OnLog);
                frm.StartPosition = p;
                if (frm.ShowDialog() == DialogResult.OK)
                {
                 
                    btnLogIn.Enabled = false;
                    btnLogOut.Enabled = true;
                    btnRegister.Enabled = false;

                }
                else
                {
                    btnLogOut.Enabled = false;
                    btnLogIn.Enabled = true;
                    btnRegister.Enabled = true;
                }

            }
        }
Exemple #3
0
        /// <summary>
        /// Get the specified resource contents
        /// </summary>
        public T Get <T>(String resourcePath, params KeyValuePair <String, Object>[] queryParms)
        {
            int retry = 0;

            while (retry++ < 3)
            {
                try
                {
                    StringBuilder queryBuilder = new StringBuilder();
                    if (queryParms != null)
                    {
                        foreach (var qp in queryParms)
                        {
                            queryBuilder.AppendFormat("{0}={1}&", qp.Key, qp.Value);
                        }
                    }
                    if (queryBuilder.Length > 0)
                    {
                        queryBuilder.Remove(queryBuilder.Length - 1, 1);
                    }
                    Uri requestUri = new Uri(String.Format("{0}/{1}?{2}", this.m_baseUri, resourcePath, queryBuilder));

                    WebRequest request = WebRequest.Create(requestUri);
                    request.Timeout = 600000;
                    request.Method  = "GET";
                    if (!String.IsNullOrEmpty(s_username))
                    {
                        request.Headers.Add("Authorization", String.Format("Basic {0}", Convert.ToBase64String(Encoding.UTF8.GetBytes(String.Format("{0}:{1}", s_username, s_password)))));
                    }
                    var response = request.GetResponse();

                    var serializer = new DataContractJsonSerializer(typeof(T));
                    T   retVal     = (T)serializer.ReadObject(response.GetResponseStream());
                    //Thread.Sleep(100); // Yeah, you're reading that right... Idk why but GIIS WS don't like to be called too quickly
                    return(retVal);
                }
                catch (WebException e)
                {
                    if ((e.Response as HttpWebResponse).StatusCode == HttpStatusCode.Unauthorized)
                    {
                        frmAuthenticate authForm = new frmAuthenticate();
                        if (authForm.ShowDialog() == DialogResult.OK)
                        {
                            s_username = authForm.Username;
                            s_password = authForm.Password;
                        }
                        else
                        {
                            throw new SecurityException("Authorization for service failed!");
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
                catch (Exception e)
                {
                    throw;
                }
            }
            throw new SecurityException("Authorization for service failed!");
        }