private string GetSerializedTring(string str) { var serializedStrAsBytes = JSonHelper.Serialize(str); var seriazedStr = Encoding.Default.GetString(serializedStrAsBytes); return(seriazedStr); }
public void WithData_ShouldSerialize() { var data = Guid.NewGuid().ToString(); var byteData = JSonHelper.Serialize(data); Assert.True(byteData.Length > 0); }
public static void SetupMockedDistributedCacheGetAsync(Mock <IDistributedCache> mock, string key, object obj) { var objAsByteArray = JSonHelper.Serialize(obj); if (obj == null) { objAsByteArray = null; } mock.Setup(m => m.GetAsync(key, It.IsAny <CancellationToken>())) .Returns(Task.FromResult(objAsByteArray)); }
public async Task AddInfinityCacheAsync(object obj, string cacheKey, string cacheType) { this.ValidateCacheKey(cacheKey); this.ValidateCacheType(cacheType); var options = this.CreateOptions(DateTime.MaxValue); var serielizedModelAsBytes = JSonHelper.Serialize(obj); await this.cache.SetAsync(cacheKey, serielizedModelAsBytes, options); this.AddKey(cacheType, cacheKey); }
public async Task AddTimedCacheAsync(object obj, string cacheKey, string cacheType, DateTime expirationDate) { this.ValidateCacheKey(cacheKey); this.ValidateCacheType(cacheType); DataValidator.ValidateMinDateTime(expirationDate, DateTime.UtcNow); var options = this.CreateOptions(expirationDate); var serielizedModelAsBytes = JSonHelper.Serialize(obj); await this.cache.SetAsync(cacheKey, serielizedModelAsBytes, options); this.AddKey(cacheType, cacheKey); }
public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } HttpResponseBase response = context.HttpContext.Response; if (!String.IsNullOrEmpty(ContentType)) { response.ContentType = ContentType; } else { response.ContentType = "application/json"; } if (ContentEncoding != null) { response.ContentEncoding = ContentEncoding; } if (Data != null) { Type type = Data.GetType(); response.Write(String.Format("{{success: true, msg: \"{0}\", data:", msg)); if (type == typeof(DataRow)) { response.Write(JSonHelper.Serialize(Data, true)); } else if (type == typeof(DataTable)) { response.Write(JSonHelper.Serialize(Data, true)); } else if (type == typeof(DataSet)) { response.Write(JSonHelper.Serialize(Data, true)); } else { JavaScriptSerializer serializer = new JavaScriptSerializer(); response.Write(serializer.Serialize(Data)); } response.Write("}"); } }
public static string CreatePostHttpResponse(string url, IDictionary <string, string> parameters, IDictionary <string, string> headersParameters) { string paraUrlCoded = JSonHelper.Serialize(parameters); //string str = HttpPost(url, paraUrlCoded); Task t = new Task(new Action(() => { //推送产品 string str = PostUrl(url, paraUrlCoded); })); t.Start(); // string str =PostUrl(url, paraUrlCoded); return(""); //获取权限 HttpWebResponse request = CreatePostHttpResponse(url, parameters, headersParameters, Encoding.UTF8); //实例化一个StreamReader对象来获取Response的GetResponseStream返回的响应的体 StreamReader Info_Reader = new StreamReader(request.GetResponseStream(), Encoding.UTF8); return(Info_Reader.ReadToEnd() + ""); }
public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary <string, string> parameters, IDictionary <string, string> headersParameters, Encoding charset) { HttpWebRequest request = null; //HTTPSQ请求 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request = WebRequest.Create(url) as HttpWebRequest; request.Timeout = 20000; request.ProtocolVersion = HttpVersion.Version10; request.Method = "POST"; if (!(headersParameters == null || headersParameters.Count == 0)) { foreach (string key in headersParameters.Keys) { request.Headers.Add(key, headersParameters[key]); } } // request.ContentType = "application/x-www-form-urlencoded"; request.ContentType = "application/json; charset=utf-8"; // string UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0"; request.UserAgent = DefaultUserAgent; //如果需要POST数据 if (!(parameters == null || parameters.Count == 0)) { //StringBuilder buffer = new StringBuilder(); //int i = 0; //foreach (string key in parameters.Keys) //{ // if (i > 0) // { // buffer.AppendFormat("&{0}={1}", key, parameters[key]); // } // else // { // buffer.AppendFormat("{0}={1}", key, parameters[key]); // } // i++; //} //byte[] data = charset.GetBytes(buffer.ToString()); //using (Stream stream = request.GetRequestStream()) //{ // stream.Write(data, 0, data.Length); //} //parameters //Newtonsoft.Json string paraUrlCoded = JSonHelper.Serialize(parameters); request.ContentLength = Encoding.UTF8.GetByteCount(paraUrlCoded); byte[] payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); request.ContentLength = payload.Length; Stream writer = request.GetRequestStream(); using (Stream stream = request.GetRequestStream()) { stream.Write(payload, 0, payload.Length); } } WebResponse response = request.GetResponse(); Stream stream1 = response.GetResponseStream(); var memoryStream = new MemoryStream(); //将基础流写入内存流 const int bufferLength = 1024; byte[] buffer1 = new byte[bufferLength]; int actual = stream1.Read(buffer1, 0, bufferLength); if (actual > 0) { memoryStream.Write(buffer1, 0, actual); } memoryStream.Position = 0; string str = ""; str = Convert.ToBase64String(memoryStream.ToArray()); return(response as HttpWebResponse); }