Пример #1
0
        static private async Task <RenRenBatchRunResponseArg> agentBatchRunReponseHandler(ICollection <RequestParameterEntity> args, string method, Uri target, ICollection <BatchRunBinder> binders)
        {
            ErrorEntity error                = null;
            string      response             = string.Empty;
            RenRenBatchRunResponseArg result = null;

            try
            {
                var agent = new HttpWebRequestAgent();
                agent.Method = method;

                foreach (var parameter in args)
                {
                    agent.AddParameters(parameter.Name, Uri.EscapeDataString(parameter.Values));
                }

                //string response = string.Empty;
                if (null != target)
                {
                    response = await agent.DownloadString(target);
                }
                else
                {
                    response = await agent.DownloadString(ConstantValue.SpecificRequestUri);
                }

                Debug.WriteLine(response);
                error = (ErrorEntity)JsonUtility.DeserializeObj(new MemoryStream(Encoding.UTF8.GetBytes(response)),
                                                                typeof(ErrorEntity));

                if (error != null && error.Error_msg != null)
                {
                    // Try to translate the error mesg
                    if (RemoteErrorMsgTranslator.Instance.Ready)
                    {
                        if (RemoteErrorMsgTranslator.Instance.ErrorTable.ContainsKey(error.Error_code))
                        {
                            var errorEntity = RemoteErrorMsgTranslator.Instance.ErrorTable[error.Error_code];
                            error.Error_msg  = errorEntity.Mesg;
                            error.Error_Type = errorEntity.Type;
                        }
                    }
                    result = new RenRenBatchRunResponseArg(error);
                }
                else
                {
                    result = new RenRenBatchRunResponseArg(response, binders);
                }
            }
            catch (Exception ex)
            {
                result = new RenRenBatchRunResponseArg(ex);
            }

            return(result);
        }
Пример #2
0
        static private async Task <ArgType> agentReponseHandler <EntityType, ArgType>(ICollection <RequestParameterEntity> args, string method, Uri target = null)
        {
            EntityType  entity   = default(EntityType);
            ErrorEntity error    = null;
            ArgType     result   = default(ArgType);
            string      response = string.Empty;

            try
            {
                var agent = new HttpWebRequestAgent();
                agent.Method = method;

                foreach (var parameter in args)
                {
                    agent.AddParameters(parameter.Name, Uri.EscapeDataString(parameter.Values));
                }

                //string response = string.Empty;
                if (null != target)
                {
                    response = await agent.DownloadString(target);
                }
                else
                {
                    response = await agent.DownloadString(ConstantValue.RequestUri);
                }

                Debug.WriteLine(response);
                entity = (EntityType)JsonUtility.DeserializeObj(new MemoryStream(Encoding.UTF8.GetBytes(response)), typeof(EntityType));

                //entity = (EntityType)JsonUtility.DeserializeObj(new MemoryStream(Encoding.Unicode.GetBytes(response)),typeof(EntityType));

                error = (ErrorEntity)JsonUtility.DeserializeObj(new MemoryStream(Encoding.UTF8.GetBytes(response)),
                                                                typeof(ErrorEntity));

                if (error != null && error.Error_msg != null)
                {
                    // Try to translate the error mesg
                    if (RemoteErrorMsgTranslator.Instance.Ready)
                    {
                        if (RemoteErrorMsgTranslator.Instance.ErrorTable.ContainsKey(error.Error_code))
                        {
                            var errorEntity = RemoteErrorMsgTranslator.Instance.ErrorTable[error.Error_code];
                            error.Error_msg  = errorEntity.Mesg;
                            error.Error_Type = errorEntity.Type;
                        }
                    }
                    result = (ArgType)Activator.CreateInstance(typeof(ArgType), error);
                }
                else if (entity != null)
                {
                    result = (ArgType)Activator.CreateInstance(typeof(ArgType), entity);
                }
                else
                {
                    result = (ArgType)Activator.CreateInstance(typeof(ArgType), new ArgumentException());
                }
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message;

                if (errorMsg.Contains("Encountered invalid character"))
                {
                    bool ifBreak = false;
                    while (!ifBreak)
                    {
                        var f = errorMsg.IndexOf("'") + 1;
                        var l = errorMsg.LastIndexOf("'");
                        var m = errorMsg.Substring(f, l - f);

                        int ret = DecoderJson(response.Replace(Convert.ToChar(m), '?'), typeof(EntityType));

                        if (ret < 0)
                        {
                            result  = (ArgType)Activator.CreateInstance(typeof(ArgType), ex);
                            ifBreak = true;
                        }
                        else if (ret > 0)
                        {
                            entity  = (EntityType)JsonUtility.DeserializeObj(new MemoryStream(Encoding.UTF8.GetBytes(response.Replace(Convert.ToChar(m), '?'))), typeof(EntityType));
                            result  = (ArgType)Activator.CreateInstance(typeof(ArgType), entity);
                            ifBreak = true;
                        }
                    }
                }
                else
                {
                    result = (ArgType)Activator.CreateInstance(typeof(ArgType), ex);
                }
            }

            return(result);
        }