示例#1
0
        public object PostCall(ServerCallParameters parameters, object targetObject, object parent, object data)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            string sdata = SerializeData(data);

            using (var client = new CookieWebClient())
            {
                if (Cookie != null)
                {
                    client.Cookies.Add(new Cookie(CookieName, Cookie, "/", new Uri(Url).Host));
                }

                var uri = new EditableUri(Url + "/api/" + parameters.Api);

                if (parameters.Lcid != 0)
                {
                    uri.Parameters["l"] = parameters.Lcid;
                }
                client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
                client.Encoding = Encoding.UTF8;

                string s;
                try
                {
                    s = client.UploadString(uri.ToString(), sdata);
                }
                catch (WebException e)
                {
                    if (ShowMessageBoxOnError)
                    {
                        var eb = new ErrorBox(e, e.GetErrorText(null));
                        eb.ShowDialog();
                    }
                    throw;
                }

                var options = new JsonUtilitiesOptions();
                options.CreateInstanceCallback = (e) =>
                {
                    var type = (Type)e.Value;
                    if (typeof(TreeItem).IsAssignableFrom(type))
                    {
                        e.Value   = Activator.CreateInstance(type, new object[] { parent });
                        e.Handled = true;
                    }
                };

                if (targetObject != null)
                {
                    JsonUtilities.Deserialize(s, targetObject, options);
                    return(null);
                }
                return(JsonUtilities.Deserialize(s));
            }
        }
示例#2
0
        public object Call(ServerCallParameters parameters, object targetObject, object parent)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            using (var client = new CookieWebClient())
            {
                if (Cookie != null)
                {
                    client.Cookies.Add(new Cookie(CookieName, Cookie, "/", new Uri(Url).Host));
                }

                var uri = new EditableUri(Url + "/api/" + parameters.Api);
                if (!string.IsNullOrWhiteSpace(parameters.Format))
                {
                    uri.Parameters["f"] = parameters.Format;
                }

                if (parameters.Lcid != 0)
                {
                    uri.Parameters["l"] = parameters.Lcid;
                }

                client.Encoding = Encoding.UTF8;

                string s;
                try
                {
                    s = client.DownloadString(uri.ToString());
                }
                catch (WebException e)
                {
                    var eb = new ErrorBox(e, e.GetErrorText(null));
                    eb.ShowDialog();
                    throw;
                }

                var options = new JsonUtilitiesOptions();
                options.CreateInstanceCallback = (e) =>
                {
                    Type type = (Type)e.Value;
                    if (typeof(TreeItem).IsAssignableFrom(type))
                    {
                        e.Value   = Activator.CreateInstance(type, new object[] { parent });
                        e.Handled = true;
                    }
                };

                if (targetObject != null)
                {
                    JsonUtilities.Deserialize(s, targetObject, options);
                    return(null);
                }
                return(JsonUtilities.Deserialize(s));
            }
        }
示例#3
0
        public static string GetErrorText(this Exception exception, JsonUtilitiesOptions options)
        {
            if (exception == null)
            {
                return(null);
            }

            if (options == null)
            {
                options = new JsonUtilitiesOptions();
            }

            string error = CodeFluentRuntimeException.GetAllMessages(exception);
            string extra = null;
            var    we    = exception as WebException;

            if (we != null && we.Response != null)
            {
                Stream stream = we.Response.GetResponseStream();
                if (stream != null && stream.CanRead)
                {
                    using (var reader = new StreamReader(stream))
                    {
                        extra = reader.ReadToEnd();
                        if (we.Response.Headers[HttpResponseHeader.ContentType] == "application/json" && extra != null)
                        {
                            options.ThrowExceptions = false;
                            var    dic = (Dictionary <string, object>)JsonUtilities.Deserialize(extra, null, options);
                            object ex;
                            if (dic.TryGetValue("Exception", out ex))
                            {
                                if (ex != null)
                                {
                                    extra = string.Format("{0}", ex);
                                }
                                else
                                {
                                    // try without deserialization
                                    options.SerializationOptions &= ~JsonSerializationOptions.UseISerializable;
                                    var dic2 = (Dictionary <string, object>)JsonUtilities.Deserialize(extra, null, options);
                                    if (dic2.TryGetValue("Exception", out ex) && ex is IDictionary <string, object> )
                                    {
                                        var dicex = (IDictionary <string, object>)ex;
                                        var sb    = new StringBuilder(Environment.NewLine);
                                        sb.AppendLine("Source: " + dicex.GetValue <string>("Source", null));
                                        sb.AppendLine("Message: " + dicex.GetValue <string>("Message", null));
                                        sb.AppendLine("Class: " + dicex.GetValue <string>("ClassName", null));
                                        sb.AppendLine(dicex.GetValue <string>("StackTraceString", null));
                                        extra = sb.ToString();
                                    }
                                    else
                                    {
                                        if (dic.TryGetValue("FullMessage", out ex))
                                        {
                                            extra = string.Format("{0}", ex);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (extra != null)
            {
                return(error + Environment.NewLine + extra);
            }

            return(error);
        }
示例#4
0
        public object PostBlobCall(ServerCallParameters parameters, object targetObject, object parent, object data, IEnumerable <IUploadableFile> files)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            if (files == null)
            {
                throw new ArgumentNullException(nameof(files));
            }

            string sdata = SerializeData(data);

            using (var httpHandler = new HttpClientHandler())
                using (var client = new HttpClient(httpHandler))
                {
                    if (Cookie != null)
                    {
                        httpHandler.CookieContainer.Add(new Cookie(CookieName, Cookie, "/", new Uri(Url).Host));
                    }
                    var uri = new EditableUri(Url + "/api/" + parameters.Api);

                    if (parameters.Lcid != 0)
                    {
                        uri.Parameters["l"] = parameters.Lcid;
                    }

                    var mpfdContent = new MultipartFormDataContent();
                    if (sdata != null)
                    {
                        mpfdContent.Add(new StringContent(sdata, Encoding.UTF8, "application/json"), "data");
                    }

                    foreach (var blob in files)
                    {
                        if (string.IsNullOrEmpty(blob.TempFilePath))
                        {
                            continue;
                        }

                        var streamContent = new StreamContent(File.Open(blob.TempFilePath, FileMode.Open, FileAccess.Read));
                        streamContent.Headers.ContentType = new MediaTypeHeaderValue(blob.ContentType);

                        mpfdContent.Add(streamContent, blob.FormName, ConvertUtilities.RemoveDiacritics(blob.FileName));
                    }

                    var message = new HttpRequestMessage(HttpMethod.Post, uri.ToString());
                    message.Content = mpfdContent;

                    string s;
                    try
                    {
                        var result = client.SendAsync(message).Result;
                        s = result.Content.ReadAsStringAsync().Result;
                        result.EnsureSuccessStatusCode();
                    }
                    catch (AggregateException agg)
                    {
                        if (ShowMessageBoxOnError)
                        {
                            agg.Handle(e =>
                            {
                                var we = e as WebException;
                                if (we != null)
                                {
                                    var eb = new ErrorBox(e, e.GetErrorText(null));
                                    eb.ShowDialog();
                                }
                                return(false);
                            });
                        }
                        throw;
                    }
                    catch (HttpRequestException e)
                    {
                        if (ShowMessageBoxOnError)
                        {
                            var eb = new ErrorBox(e, e.GetErrorText(null));
                            eb.ShowDialog();
                        }
                        throw;
                    }

                    var options = new JsonUtilitiesOptions();
                    options.CreateInstanceCallback = (e) =>
                    {
                        var type = (Type)e.Value;
                        if (typeof(TreeItem).IsAssignableFrom(type))
                        {
                            e.Value   = Activator.CreateInstance(type, new object[] { parent });
                            e.Handled = true;
                        }
                    };

                    if (targetObject != null)
                    {
                        JsonUtilities.Deserialize(s, targetObject, options);
                        return(null);
                    }
                    return(JsonUtilities.Deserialize(s));
                }
        }