Пример #1
0
        public static async Task <StringResponse> FormPost(string url, HttpContent content)
        {
            var response = new StringResponse()
            {
            };

            try
            {
                using (var client = GetClient())
                {
                    var postResponse = await client.PostAsync(url, content);

                    postResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
                    postResponse.EnsureSuccessStatusCode();

                    var raw = await postResponse.Content.ReadAsStringAsync();

                    if (raw != null)
                    {
                        response.Response = raw;
                        response.Success  = true;
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ConsoleWrite();
                response.Error = ex;
            }
            return(response);
        }
Пример #2
0
        public static T ConvertTo <T>(this StringResponse str) where T : struct
        {
            object result = null;
            var    code   = Type.GetTypeCode(typeof(T));

            switch (code)
            {
            case TypeCode.Int32:
                result = JsonConvert.DeserializeObject <int>(str.Response);
                break;

            case TypeCode.Int16:
                result = JsonConvert.DeserializeObject <short>(str.Response);
                break;

            case TypeCode.Int64:
                result = JsonConvert.DeserializeObject <long>(str.Response);
                break;

            case TypeCode.String:
                result = JsonConvert.DeserializeObject <string>(str.Response);
                break;

            case TypeCode.Boolean:
                result = JsonConvert.DeserializeObject <bool>(str.Response);
                break;

            case TypeCode.Double:
                result = JsonConvert.DeserializeObject <double>(str.Response);
                break;

            case TypeCode.Decimal:
                result = JsonConvert.DeserializeObject <decimal>(str.Response);
                break;

            case TypeCode.Byte:
                result = JsonConvert.DeserializeObject <Byte>(str.Response);
                break;

            case TypeCode.DateTime:
                result = JsonConvert.DeserializeObject <DateTime>(str.Response);
                break;

            case TypeCode.Single:
                result = JsonConvert.DeserializeObject <Single>(str.Response);
                break;
            }
            return((T)result);
        }
Пример #3
0
        public static async Task <StringResponse> GetStringAsync(string contentName)
        {
            await fileStoreLock.WaitAsync();

            var response = new StringResponse {
                Success = false
            };

            return(await Task.Run(() =>
            {
                try
                {
                    using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (isoStorage.FileExists(contentName))
                        {
                            try
                            {
                                using (var s = isoStorage.OpenFile(contentName, FileMode.OpenOrCreate))
                                {
                                    using (var sr = new StreamReader(s))
                                    {
                                        var content = sr.ReadToEnd();
                                        sr.Close();
                                        response.Success = true;
                                        response.Response = content;
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                response.Error = ex;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    response.Error = ex;
                }
                finally
                {
                    fileStoreLock.Release();
                }
                return response;
            }));
        }