Ejemplo n.º 1
0
        public static async Task <String> getResponseString(String Path)
        {
            HttpResponseMessage response;
            var client    = new HttpClient(new LoggingHandler(new HttpClientHandler()));
            var byteArray = Encoding.ASCII.GetBytes(APIConstants.getCredentials());

            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
            client.BaseAddress = new Uri(Path);
            try
            {
                response = client.GetAsync(Path).Result;

                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine("DELETE SUCCESS\n");
                }
                else
                {
                    Console.WriteLine("DELETE ERROR\n");
                }

                return(await response.Content.ReadAsStringAsync());
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: '{0}'", e);
            }

            return("");
        }
Ejemplo n.º 2
0
        public static async Task <String> postResponseString(String Path, String JsonString)
        {
            HttpResponseMessage     response;
            CancellationTokenSource cts;

            cts = new CancellationTokenSource();
            HttpClient client    = new HttpClient(new LoggingHandler(new HttpClientHandler()));
            var        byteArray = Encoding.ASCII.GetBytes(APIConstants.getCredentials());

            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
            client.DefaultRequestHeaders
            .Accept
            .Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(Path));

            request.Content = new StringContent(JsonString,
                                                Encoding.UTF8,
                                                "application/json");

            try
            {
                response = await client.SendAsync(request);

                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine("POST SUCCESS\n");
                }
                else
                {
                    Console.WriteLine("POST ERROR\n");
                }

                return(await response.Content.ReadAsStringAsync());
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: '{0}'", e);
            }

            return("");
        }