Ejemplo n.º 1
0
        /// <summary>
        /// send http post to server
        /// </summary>
        /// <param name="requestUrl"></param>
        /// <param name="values"></param>
        /// <param name="timeOut"></param>
        /// <returns>json data</returns>
        public static string SendHttpPostRequest(String requestUrl, Dictionary <string, string> values, int timeOut /*milliseconds*/, bool isEncryt = true)
        {
            try
            {
                string postData   = string.Empty; //format key1=value1&key2=value2
                bool   isTheFirst = true;
                foreach (var item in values)
                {
                    if (isTheFirst == true)
                    {
                        postData  += string.Format("{0}={1}", item.Key, item.Value);
                        isTheFirst = false;
                    }
                    else
                    {
                        postData += string.Format("&{0}={1}", item.Key, item.Value);
                    }
                }

                if (isEncryt == true)
                {
                    postData = CryptoAesAPI.Encrypt(postData);
                }

                string ss = CryptoAesAPI.Decrypt(postData);

                byte[]     data    = Encoding.ASCII.GetBytes(postData);
                WebRequest request = WebRequest.Create(requestUrl);
                request.Method        = "POST";
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                request.Timeout       = timeOut;

                using (var stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }

                var          response           = (HttpWebResponse)request.GetResponse();
                Stream       dataStream         = response.GetResponseStream();
                StreamReader reader             = new StreamReader(dataStream);
                String       responseFromServer = reader.ReadToEnd();
                reader.Close();
                dataStream.Close();
                response.Close();

                return(responseFromServer);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
        private bool ConnectServerProc(Server server, string data)
        {
            if (data == null)
            {
                Toast.ShowInThread(server.ServerName + ": " + StringResources.Find("CONNECT_SERVER_FAILED"), TOAST_LENGTH.LONG);
                return(false);
            }

            try
            {
                data = CryptoAesAPI.Decrypt(data);
                ConnectServerJson connectServerJson = JsonConvert.DeserializeObject <ConnectServerJson>(data);

                if (connectServerJson.msgId != (int)ENUM_MSG_ID.MSG_ID_OK || connectServerJson.token == null ||
                    connectServerJson.listChannel == null)
                {
                    switch (connectServerJson.msgId)
                    {
                    case (int)ENUM_MSG_ID.LOGIN_AUTH_ERROR:
                        Toast.ShowInThread(server.ServerName + ": " + StringResources.Find("LOGIN_SERVER_FAILED"), TOAST_LENGTH.LONG);
                        break;

                    default:
                        Toast.ShowInThread(server.ServerName + ": " + StringResources.Find("LOGIN_SERVER_FAILED"), TOAST_LENGTH.LONG);
                        //ENUM_MSG_ID msgId = (ENUM_MSG_ID)connectServerJson.msgId;
                        //Toast.ShowInThread(server.ServerName + ": " + msgId.ToString(), TOAST_LENGTH.LONG);
                        break;
                    }

                    return(false);
                }

                List <Channel> listChannel = new List <Channel>();
                foreach (var channel in connectServerJson.listChannel)
                {
                    channel.cameraDesc  = CryptoAesAPI.ConvertAscIIToUTF8(channel.cameraDesc);
                    channel.cameraName  = CryptoAesAPI.ConvertAscIIToUTF8(channel.cameraName);
                    channel.channelName = CryptoAesAPI.ConvertAscIIToUTF8(channel.channelName);
                    listChannel.Add(channel);
                }

                int serverIndex = 0;
                lock (lockCS)
                {
                    for (int i = 0; i < Global.MAX_SERVER_CONNECTING_SUPPORT; i++)
                    {
                        if (serverSiteConnectingList.ContainsKey(i) == false)
                        {
                            serverIndex = i;
                            serverSiteConnectingList.Add(serverIndex,
                                                         new ServerSiteConnecting(serverIndex, server, connectServerJson.token,
                                                                                  listChannel));
                            break;
                        }
                    }
                }

                if (ServerConnected != null)
                {
                    ServerConnected(serverIndex, true);
                }

                return(true);
            }
            catch
            {
                Toast.ShowInThread(server.ServerName + ": " + StringResources.Find("CONNECT_SERVER_FAILED"), TOAST_LENGTH.LONG);
                return(false);
            }
        }