Ejemplo n.º 1
0
        public T Read()
        {
            StringBuilder sb = new StringBuilder();

            try
            {
                using (var fs = File.Open(_filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    var bytes = new byte[1024];

                    fs.Position = 0;

                    while (true)
                    {
                        var count = fs.Read(bytes, 0, bytes.Length);

                        if (count == 0)
                        {
                            break;
                        }

                        if (count == bytes.Length)
                        {
                            sb.Append(Encoding.UTF8.GetString(bytes));
                        }
                        else
                        {
                            sb.Append(Encoding.UTF8.GetString(bytes.AsSpan().Slice(0, count).ToArray()));
                            break;
                        }
                    }
                }
            }
            catch (Exception ex) { }

            var json = sb.ToString();

            if (string.IsNullOrWhiteSpace(json))
            {
                return(default(T));
            }

            return(SerializeHelper.Deserialize <T>(json));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// get请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="headers"></param>
        /// <param name="timeOut"></param>
        /// <returns></returns>
        public static async Task <T> GetViewModelAsync <T>(string url, Dictionary <string, string> headers = null, int timeOut = 30 * 1000)
        {
            if (headers == null)
            {
                headers = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            }

            if (!headers.ContainsKey("Content-Type"))
            {
                headers.Add("Content-Type", "application/json; charset=UTF-8");
            }

            var json = await GetJsonAsync(url, headers, timeOut);

            if (string.IsNullOrEmpty(json))
            {
                return(default(T));
            }
            return(SerializeHelper.Deserialize <T>(json));
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 带重试次数的post
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="url"></param>
 /// <param name="json"></param>
 /// <param name="tryTimes"></param>
 /// <param name="headers"></param>
 /// <param name="timeOut"></param>
 /// <returns></returns>
 public static T TryPost <T>(string url, string json, int tryTimes = 10, WebHeaderCollection headers = null, int timeOut = 120 * 1000)
 {
     return(SerializeHelper.Deserialize <T>(TryPost(url, json, tryTimes, headers, timeOut)));
 }