Esempio n. 1
0
        void Bw_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs args)
        {
            if (args.Error != null)
            {
                DisplayAlert("Error", args.Error.GetFullInfo(), "确定");
                return;
            }

            if (args.Result == null)
            {
                DisplayAlert("Error", "SOAPResult为空", "确定");
                return;
            }

            Util.WebService.SOAPResult soapResult = args.Result as Util.WebService.SOAPResult;

            if (soapResult.IsComplete == false)
            {
                DisplayAlert("Error", soapResult.ExceptionInfo, "确定");
            }
            else if (soapResult.IsSuccess == false)
            {
                DisplayAlert("Error", soapResult.BusinessExceptionInfo, "确定");
            }
            else
            {
                DisplayAlert("提示", "执行成功。", "确定");
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 缺点 : 应该把 BackgroundWorker 用于mClient.UploadString(), 而不是在 WebService 具体方法中创建 BackgroundWorker
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public Util.WebService.SOAPResult ExecuteV1(Uri uri, Util.WebService.RequestData requestData)
        {
            string data = string.Empty;

            try
            {
                data = mClient.UploadString(uri, "POST", Util.JsonUtils.SerializeObject(requestData));
            }
            catch (System.Net.WebException webEx)
            {
                string msg = string.Format("{0}", webEx.Message);
                System.Diagnostics.Debug.WriteLine(msg);
            }
            finally
            {
                mClient.Dispose();
            }

            Util.WebService.SOAPResult soapResult = Util.JsonUtils.DeserializeObject <Util.WebService.SOAPResult>(data);
            return(soapResult);
        }
Esempio n. 3
0
        /// <summary>
        /// 1 修复 page, handle 的处理方式
        /// 2 去掉 string.FormatWith 的字符串转换方式
        /// </summary>
        /// <param name="uri">Web Service Uri</param>
        /// <param name="requestData">参数</param>
        /// <param name="page">UI Page</param>
        /// <param name="handle">UI Handler</param>
        /// <param name="isCompress">是否压缩(默认不压缩)</param>
        /// <param name="isEncrypt">是否加密(默认不加密)</param>
        public void ExecuteV5
        (
            Uri uri,
            Util.WebService.RequestData requestData,
            Xamarin.Forms.Page page = null,
            Action <Util.WebService.SOAPResult> handle = null,
            bool isCompress = false,
            bool isEncrypt  = false
        )
        {
            var current = Xamarin.Essentials.Connectivity.NetworkAccess;

            if (current == Xamarin.Essentials.NetworkAccess.None)
            {
                string msg = "检测设备没有可用的网络,请开启 数据 或 Wifi。";
                if (handle == null)
                {
                    if (page == null)
                    {
                        System.Diagnostics.Debug.WriteLine(msg);
                    }
                    else
                    {
                        page.DisplayAlert("Error", msg, "确定");
                    }
                }
                else // handle != null
                {
                    Util.WebService.SOAPResult soapResultHasError = new Util.WebService.SOAPResult();

                    soapResultHasError.IsComplete    = false;
                    soapResultHasError.ExceptionInfo = $"{msg}";
                    soapResultHasError.IsSuccess     = false;
                    handle.Invoke(soapResultHasError);
                    return;
                }
                return;
            }

            System.ComponentModel.BackgroundWorker bw = new System.ComponentModel.BackgroundWorker();

            bw.DoWork += (s, e) =>
            {
                string data = string.Empty;
                try
                {
                    #region 先压缩, 后加密

                    requestData.IsCompress = isCompress;
                    if (requestData.IsCompress)
                    {
                        requestData.CompressType = "GZip"; // 默认使用GZip压缩
                        for (int i = 0; i < requestData.JsonArgs.Count; i++)
                        {
                            requestData.JsonArgs[i] = requestData.JsonArgs[i].GZip_Compress2String();
                        }
                    }

                    requestData.IsEncrypt = isEncrypt;
                    if (requestData.IsEncrypt)
                    {
                        requestData.EncryptType = "RSA"; // 默认使用RSA加密
                        for (int i = 0; i < requestData.JsonArgs.Count; i++)
                        {
                            requestData.JsonArgs[i] = requestData.JsonArgs[i].RSA_Encrypt();
                        }
                    }

                    #endregion

                    data = mClient.UploadString(uri, "POST", Util.JsonUtils.SerializeObject(requestData));
                }
                catch (System.Net.WebException webEx)
                {
                    string msg = $"执行 {requestData.MethodName} 发生未知错误";
                    throw new Exception(msg, webEx);
                }
                finally
                {
                    mClient.Dispose();
                }

                Util.WebService.SOAPResult soapResult = Util.JsonUtils.DeserializeObject <Util.WebService.SOAPResult>(data);
                e.Result = soapResult;
            };


            bw.RunWorkerCompleted += async(obj, args) =>
            {
                if (handle == null)
                {
                    if (args.Error != null)
                    {
                        string msg = $"{args.Error.GetFullInfo()}";
                        if (page == null)
                        {
                            System.Diagnostics.Debug.WriteLine(msg);
                        }
                        else
                        {
                            await page.DisplayAlert("Error", msg, "确定");
                        }
                        return;
                    }

                    if (args.Result == null)
                    {
                        string msg = $"执行 {requestData.MethodName} 发生未知错误:args.Result为空值";
                        if (page == null)
                        {
                            System.Diagnostics.Debug.WriteLine(msg);
                        }
                        else
                        {
                            await page.DisplayAlert("Error", msg, "确定");
                        }
                        return;
                    }

                    // 程序员没有传入返回结果的后续处理, 就此结束
                    return;
                }
                else // Handle != null
                {
                    if (args.Error != null)
                    {
                        Util.WebService.SOAPResult soapResultHasError = new Util.WebService.SOAPResult();

                        soapResultHasError.IsComplete    = false;
                        soapResultHasError.ExceptionInfo = args.Error.GetFullInfo();
                        soapResultHasError.IsSuccess     = false;
                        handle.Invoke(soapResultHasError);
                        return;
                    }

                    if (args.Result == null)
                    {
                        string msg = $"执行 {requestData.MethodName} 发生未知错误:args.Result为空值";

                        Util.WebService.SOAPResult soapResultHasError = new Util.WebService.SOAPResult();

                        soapResultHasError.IsComplete    = false;
                        soapResultHasError.ExceptionInfo = $"{msg}\r\n{args.Error.GetFullInfo()}";
                        soapResultHasError.IsSuccess     = false;
                        handle.Invoke(soapResultHasError);
                        return;
                    }

                    Util.WebService.SOAPResult soapResult = args.Result as Util.WebService.SOAPResult;

                    #region soapResult.ReturnObjectJson 解密 & 解压

                    // 1 是否经过加密, 若是进行解密
                    if (soapResult.IsEncrypt == true)
                    {
                        switch (soapResult.EncryptType.ToUpper())
                        {
                        case "RSA": soapResult.ReturnObjectJson = soapResult.ReturnObjectJson.RSA_Decrypt(); break;

                        case "DES": soapResult.ReturnObjectJson = soapResult.ReturnObjectJson.DES_Decrypt(); break;

                        default: break;
                        }

                        soapResult.IsEncrypt = false; // 解密后设置为False
                    }

                    // 2 是否经过压缩, 若是进行解压
                    if (soapResult.IsCompress == true)
                    {
                        switch (soapResult.CompressType.ToUpper())
                        {
                        case "GZIP": soapResult.ReturnObjectJson = soapResult.ReturnObjectJson.GZip_Decompress2String(); break;

                        default: break;
                        }

                        soapResult.IsEncrypt = false; // 解压后设置为False
                    }

                    #endregion

                    handle.Invoke(soapResult);
                }
            };

            bw.RunWorkerAsync();
        }
Esempio n. 4
0
        /// <summary>
        /// 待优化: 支持压缩与加密 requestData.JsonArgs
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="requestData"></param>
        /// <param name="page"></param>
        /// <param name="handle"></param>
        public void ExecuteV3
        (
            Uri uri,
            Util.WebService.RequestData requestData,
            Xamarin.Forms.Page page = null,
            Action <Util.WebService.SOAPResult> handle = null
        )
        {
            //var current = Xamarin.Essentials.Connectivity.NetworkAccess;
            //if (current == Xamarin.Essentials.NetworkAccess.None)
            //{
            //    string msg = "检测设备没有可用的网络,请开启 数据 或 Wifi。".FormatWith(requestData.MethodName);
            //    if (page == null)
            //    {
            //        System.Diagnostics.Debug.WriteLine(msg);
            //    }
            //    else
            //    {
            //        page.DisplayAlert("Error", msg, "确定");
            //    }
            //    return;
            //}


            System.ComponentModel.BackgroundWorker bw = new System.ComponentModel.BackgroundWorker();

            bw.DoWork += (s, e) =>
            {
                string data = string.Empty;
                try
                {
                    data = mClient.UploadString(uri, "POST", Util.JsonUtils.SerializeObject(requestData));
                }
                catch (System.Net.WebException webEx)
                {
                    string msg = string.Format("{0}", webEx.Message);
                    System.Diagnostics.Debug.WriteLine(msg);
                }
                finally
                {
                    mClient.Dispose();
                }

                Util.WebService.SOAPResult soapResult = Util.JsonUtils.DeserializeObject <Util.WebService.SOAPResult>(data);
                e.Result = soapResult;
            };


            bw.RunWorkerCompleted += (obj, args) =>
            {
                if (args.Error != null)
                {
                    string msg = "{0}".FormatWith(args.Error.GetFullInfo());
                    if (page == null)
                    {
                        System.Diagnostics.Debug.WriteLine(msg);
                    }
                    else
                    {
                        page.DisplayAlert("Error", msg, "确定");
                    }
                    return;
                }

                if (args.Result == null)
                {
                    string msg = "执行 {0} 发生未知错误:args.Result为空值".FormatWith(requestData.MethodName);
                    if (page == null)
                    {
                        System.Diagnostics.Debug.WriteLine(msg);
                    }
                    else
                    {
                        page.DisplayAlert("Error", msg, "确定");
                    }
                    return;
                }

                if (handle == null)
                {
                    return;
                }

                Util.WebService.SOAPResult soapResult = args.Result as Util.WebService.SOAPResult;
                handle.Invoke(soapResult);
            };

            bw.RunWorkerAsync();
        }
Esempio n. 5
0
        /// <summary>
        /// 缺点 : 逻辑处理不方便, 报错时页面不知道接下去要做什么
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="requestData"></param>
        /// <param name="page"></param>
        /// <param name="handle"></param>
        public void ExecuteV2
        (
            Uri uri,
            Util.WebService.RequestData requestData,
            Xamarin.Forms.Page page = null,
            Action <string> handle  = null
        )
        {
            System.ComponentModel.BackgroundWorker bw = new System.ComponentModel.BackgroundWorker();

            bw.DoWork += (s, e) =>
            {
                string data = string.Empty;
                try
                {
                    data = mClient.UploadString(uri, "POST", Util.JsonUtils.SerializeObject(requestData));
                }
                catch (System.Net.WebException webEx)
                {
                    string msg = string.Format("{0}", webEx.Message);
                    System.Diagnostics.Debug.WriteLine(msg);
                }
                finally
                {
                    mClient.Dispose();
                }

                Util.WebService.SOAPResult soapResult = Util.JsonUtils.DeserializeObject <Util.WebService.SOAPResult>(data);
                e.Result = soapResult;
            };


            bw.RunWorkerCompleted += (obj, args) =>
            {
                if (args.Error != null)
                {
                    string msg = "{0}".FormatWith(args.Error.GetFullInfo());
                    if (page == null)
                    {
                        System.Diagnostics.Debug.WriteLine(msg);
                    }
                    else
                    {
                        page.DisplayAlert("Error", msg, "确定");
                    }
                    return;
                }

                if (args.Result == null)
                {
                    string msg = "执行 {0} 发生未知错误:args.Result为空值".FormatWith(requestData.MethodName);
                    if (page == null)
                    {
                        System.Diagnostics.Debug.WriteLine(msg);
                    }
                    else
                    {
                        page.DisplayAlert("Error", msg, "确定");
                    }
                    return;
                }


                Util.WebService.SOAPResult soapResult = args.Result as Util.WebService.SOAPResult;
                if (soapResult.IsComplete == false)
                {
                    if (page == null)
                    {
                        System.Diagnostics.Debug.WriteLine(soapResult.ExceptionInfo);
                    }
                    else
                    {
                        page.DisplayAlert("Error", soapResult.ExceptionInfo, "确定");
                    }
                    return;
                }
                else if (soapResult.IsSuccess == false)
                {
                    if (page == null)
                    {
                        System.Diagnostics.Debug.WriteLine(soapResult.BusinessExceptionInfo);
                    }
                    else
                    {
                        page.DisplayAlert("Error", soapResult.BusinessExceptionInfo, "确定");
                    }
                    return;
                }
                else
                {
                    if (handle != null)
                    {
                        handle.Invoke(soapResult.ReturnObjectJson);
                    }
                }
            };

            bw.RunWorkerAsync();
        }