コード例 #1
0
        public void BeginGetLatestVersion(AsyncVersionCheck callback)
        {
            try {
                var req = HttpWebRequest.Create(HostUrl + "update/latest/") as HttpWebRequest;
                req.Timeout = 15 * 1000;

                req.BeginGetResponse(delegate(IAsyncResult result) {
                    try {
                        var resp = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
                        using (var sr = new StreamReader(resp.GetResponseStream( ))) {
                            using (var jr = new JsonTextReader(sr)) {
                                var serializer = JsonSerializationBuilder.Build().Create();
                                var version    = serializer.Deserialize <SoftwareRelease>(jr);
                                if (callback != null)
                                {
                                    callback(version);
                                }
                            }
                        }
                    } catch (Exception e) {
                        this.LogWarn(e.Message, e);
                    }
                }, req);
            } catch (Exception ex) {
                this.LogWarn(ex.Message, ex);
            }
        }
コード例 #2
0
        /// <summary>
        /// <summary>
        /// Enables processing of the result of an action method by a custom type that inherits from the <see cref="T:System.Web.Mvc.ActionResult" /> class.
        /// </summary>
        /// <param name="context">The context within which the result is executed.</param>
        public override void ExecuteResult(System.Web.Mvc.ControllerContext context)
        {
            var response = context.HttpContext.Response;
            var request  = context.HttpContext.Request;
            var callback = this.Callback ?? "callback";

            // check if the callback is a querystring param, or is it the actual callback name.
            if (request.QueryString.AllKeys.Any(s => string.Compare(s, callback, StringComparison.InvariantCultureIgnoreCase) == 0))
            {
                callback = request.QueryString[callback];
            }

            if (!IsValidJsonPIdentifier(callback))
            {
                throw new ArgumentException(Resources.JsonP_InvalidIdentifier.With(callback.HtmlEncode( )));
            }

            response.ContentType     = "application/javascript";
            response.ContentEncoding = this.Encoding;
            using (var sw = new StreamWriter(response.OutputStream)) {
                using (var jw = new JsonTextWriter(sw)) {
                    jw.WriteRaw(callback.Trim() + "(");
                    JsonSerializationBuilder.Build().Create().Serialize(jw, this.Data);
                    jw.WriteRaw(");");
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Enables processing of the result of an action method by a custom type that inherits from the <see cref="T:System.Web.Mvc.ActionResult" /> class.
        /// </summary>
        /// <param name="context">The context in which the result is executed. The context information includes the controller, HTTP content, request context, and route data.</param>
        public override void ExecuteResult(ControllerContext context)
        {
            var response = context.HttpContext.Response;

            response.ContentType = "application/bson";
            using (var jw = new BsonWriter(response.OutputStream)) {
                JsonSerializationBuilder.Build().Create().Serialize(jw, this.Data);
            }
        }
コード例 #4
0
        /// <summary>
        /// Enables processing of the result of an action method by a custom type that inherits from the <see cref="T:System.Web.Mvc.ActionResult" /> class.
        /// </summary>
        /// <param name="context">The context within which the result is executed.</param>
        public override void ExecuteResult(ControllerContext context)
        {
            var response = context.HttpContext.Response;

            response.ContentType     = "application/json";
            response.ContentEncoding = this.Encoding;

            using (var sw = new StreamWriter(response.OutputStream)) {
                using (var jw = new JsonTextWriter(sw)) {
                    JsonSerializationBuilder.Build( ).Create( ).Serialize(jw, this.Data);
                }
            }
        }
コード例 #5
0
        public void IgnoresNullsAndDoesNotIgnoreDefaultValues()
        {
            var sut = new JsonSerializationBuilder()
                      .UsePrettyFormat()
                      .Build();

            var r     = new Response <BlogInfo>(HttpStatusCode.BadRequest, "something bad happend");
            var bytes = sut.Serialize(r);
            var str   = Encoding.UTF8.GetString(bytes);

            Assert.Contains("isSuccess", str);
            Assert.DoesNotContain("exception", str);
        }
コード例 #6
0
 private T DeserializeStream <T>(string url)
 {
     using (var stream = GetDataStream(Urls.SHOPPING)) {
         if (stream != null)
         {
             using (var sr = new StreamReader(stream)) {
                 using (var jr = new JsonTextReader(sr)) {
                     var result = JsonSerializationBuilder.Build().Create().Deserialize <T>(jr);
                     return(result);
                 }
             }
         }
     }
     return(default(T));
 }
コード例 #7
0
        private T SendPayload <T>(string url, string method, T payload)
        {
            try {
                var sb = new StringBuilder();
                // this finds the csrf cookie. if it exists, it will be added as a header
                var csrf = FindCookie("csrf");

                foreach (Cookie f in Cookies.GetCookies(new Uri(Defaults.ORIGIN)))
                {
                    Console.WriteLine("{0}:{1}", f.Name, f.Value);
                }

                var req = HttpWebRequestBuilder.Build(new Uri(Urls.BASE + url))
                          .Setting(x => x.UserAgent, Defaults.USERAGENT)
                          .Setting(x => x.Referer, Defaults.REFERER)
                          .Setting(x => x.CookieContainer, Cookies)
                          .Setting(x => x.Accept, "application/json, text/javascript, */*; q=0.01")
                          .Setting(x => x.ContentType, "application/json")
                          .Setting(x => x.Method, method.Require())
                          .SetHeader("Origin", Defaults.ORIGIN)
                          .SetHeader("csrf", csrf)
                          .Create();

                var serializer = JsonSerializationBuilder.Build().Create();

                using (var serializedDataStream = new MemoryStream()) {
                    using (var streamWriter = new StreamWriter(serializedDataStream)) {
                        serializer.Serialize(streamWriter, payload);
                        streamWriter.Flush();
                        // move to start
                        serializedDataStream.Position = 0;
                        using (var rs = req.GetRequestStream()) {
                            serializedDataStream.CopyTo(rs);
                        }

                        var resp = req.GetResponse() as HttpWebResponse;
                        if (resp.StatusCode != HttpStatusCode.OK)
                        {
                            throw new WebException(resp.StatusDescription, (WebExceptionStatus)resp.StatusCode);
                        }
                    }
                }
                return(payload);
            } catch (WebException wex) {
                Console.Error.WriteLine(wex.Message);
            }
            return(default(T));
        }
コード例 #8
0
        public void Shortenning()
        {
            var sut = new JsonSerializationBuilder()
                      .UseShortTypeNames(typeof(BlogInfo), typeof(BlogPost))
                      .UsePrettyFormat()
                      .Build();

            var data  = BlogInfo.Create();
            var bytes = sut.Serialize(data);
            var s     = Encoding.UTF8.GetString(bytes);

            Assert.Contains("BP.", s);

            var data2 = sut.Deserialize(bytes);

            Assert.True(data2 is BlogInfo);
        }
コード例 #9
0
        public void Shortenning2()
        {
            var sut = new JsonSerializationBuilder()
                      .UseShortTypeNames(typeof(BlogInfo), typeof(BlogPost), typeof(Dictionary <string, object>))
                      .UsePrettyFormat()
                      .Build();

            var data = new Dictionary <string, object>
            {
                { "AAA", BlogInfo.Create() },
                { "BBB", 125.95 },
                { "CCC", new[] { "fff", "eee" } },
                { "DDD", BlogPost.Create() },
            };
            var bytes = sut.Serialize(data);
            var s     = Encoding.UTF8.GetString(bytes);

            var data2 = sut.Deserialize(bytes);
        }
コード例 #10
0
        public SoftwareRelease GetLatestVersion( )
        {
            try {
                var req  = HttpWebRequest.Create(HostUrl + "update/latest/") as HttpWebRequest;
                var resp = req.GetResponse( ) as HttpWebResponse;

                using (var sr = new StreamReader(resp.GetResponseStream( ))) {
                    using (var jr = new JsonTextReader(sr)) {
                        var serializer = JsonSerializationBuilder.Build().Create();
                        return(serializer.Deserialize <SoftwareRelease> (jr));
                    }
                }
            } catch (Exception ex) {
                this.LogWarn(ex.Message, ex);
                return(new SoftwareRelease {
                    ID = 0,
                    Description = "N/A",
                    Name = "N/A",
                    TimeStamp = DateTime.UtcNow,
                    Version = new Version(0, 0, 0, 0)
                });
            }
        }