Exemplo n.º 1
0
Arquivo: Json.cs Projeto: onami/DL-770
        public static RpcResponse Deserialize(string s)
        {
            var response = new RpcResponse();
            var match = new Regex("{\"result\":\"?(.*?)\"?,\"error\":(.*?)}").Match(s);
            if (match.Success)
            {
                if (match.Groups[1].Value == "null")
                    response.result = null;
                else
                    response.result = match.Groups[1].Value;

                if (match.Groups[2].Value == "null")
                    response.status = RpcResponse.StatusCode.Ok;
                else
                {
                    try
                    {
                        response.status = (RpcResponse.StatusCode)Int32.Parse(match.Groups[2].Value);
                    }
                    catch (Exception e)
                    {
                        response.result = e.Message;
                        response.status = RpcResponse.StatusCode.InternalServerError;
                        return response;
                    }
                }
            }

            return response;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Отправка и обработка данных. Обёртка над UploadString.
        /// </summary>
        /// <param name="url">Метод</param>
        /// <param name="data">Параметры</param>
        RpcResponse SendPostData(string url, string data)
        {
            try
            {
                var byteArray = UniEncoding.GetBytes(data);
                var webRequest = (HttpWebRequest)WebRequest.Create(Configuration.Server + url);
                //ставим в null; в противном случае webRequest начинает поиск прокси и тратит кучу времени
                //На DL770 падает
                //webRequest.Proxy = null;
                webRequest.Method = "POST";
                webRequest.AllowAutoRedirect = false;
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.ContentLength = byteArray.Length;
                webRequest.Timeout = 5000;
                var webpageStream = webRequest.GetRequestStream();
                webpageStream.Write(byteArray, 0, byteArray.Length);
                webpageStream.Close();

                //обработка
                using (var response = webRequest.GetResponse())
                {
                    var responseStr = new StreamReader(response.GetResponseStream()).ReadToEnd();
                    //MessageBox.Show(responseStr);
                    //Использовал самописный десериализатор, т.к. CodeBetter, цитирую:
                    //On the whole I found it works; From memory it does not deal with nulls nicely,
                    //and I think I had to tweak datetime serialisation to make it work the way
                    //other json serialisers do.
                    //
                    //Иными словами, Converter.Deserialize<RpcResponse>(responseStr) валится на "result":"null"
                    return RfidJson.Deserialize(responseStr);
                }
            }

            catch (Exception e)
            {
                var response = new RpcResponse();
                response.result = e.Message;
                response.status = RpcResponse.StatusCode.InternalServerError;
                return response;
            }
        }