示例#1
0
        public async Task <bool> DeleteFromServer(object o, DataType d)
        {
            clientHandler.UseCookies = true;
            var postfixCreator = PostfixFactory.Generate(d);
            var postfix        = postfixCreator.Generate(o);

            var rep = await client.DeleteAsync($"{UrlFactory.Generate(d)}/{postfix}");

            Debug.WriteLine(rep.StatusCode, "Status, delete");
            Debug.WriteLine(rep);
            return(rep.IsSuccessStatusCode);
        }
示例#2
0
        public async Task <bool> SendDataReturnIsValid(object dataToSend, DataType dataType)
        {
            var parser = ParserFactory.Generate(dataType);
            var data   = parser.ParsedData(dataToSend);

            if (!(dataType == DataType.Picture || dataType == DataType.Video))
            {
                Debug.WriteLine(data, "JSON_DATA:");
            }

            clientHandler.UseCookies = true;

            HttpResponseMessage response = null;

            try
            {
                response = await client.PostAsync(UrlFactory.Generate(dataType),
                                                  new StringContent(data, Encoding.UTF8, "application/json"));
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(false);
            }

            try
            {
                response.EnsureSuccessStatusCode();
            }
            catch (HttpRequestException e)
            {
                errorHandler?.Handle(response);

                Debug.WriteLine($"{e.Message}",
                                "HttpRequestException");
                return(false);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message, "Servercommunicator exception:");
                return(false);
            }
#if DEBUG
            try
            {
                Response = await response.Content.ReadAsStringAsync();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(false);
            }

            Debug.WriteLine(Response, "SERVER_RESPONSE:");
#endif

            try
            {
                var cookieUrl       = new Uri(UrlFactory.Generate(dataType));
                var responseCookies = clientHandler.CookieContainer.GetCookies(cookieUrl);

                if (dataHandler != null)
                {
                    dataHandler.LatestMessage         = response;
                    dataHandler.LatestReceivedCookies = responseCookies;
                }
            }
            catch (NullReferenceException e)
            {
                Debug.WriteLine(e.Message, "CookieError");
            }

            return(response.IsSuccessStatusCode);
        }