Exemple #1
0
        public async Task <HttpClientResponse> Request(string Method, string Url, dynamic Data = null, Dictionary <string, string> Headers = null)
        {
            HttpClientResponse result = new HttpClientResponse();
            int attempts = 0;

            Console.WriteLine("Request running...");
            while (attempts < LoginAttempts)
            {
                ReadCookies();
                Console.WriteLine("Request Attempt: {0}", attempts + 1);

                var Headers_ = new Dictionary <string, string>()
                {
                    { "BPMCSRF", BPMCSRF }
                };
                Dictionary <string, string> temp_ = new Dictionary <string, string>();
                if (Headers != null && Headers.Count > 0)
                {
                    temp_ = Headers_.Concat(Headers).GroupBy(d => d.Key).ToDictionary(d => d.Key, d => d.First().Value);
                }

                var response = await HttpClientRequest(Method, Url, Data, (temp_.Count > 0 ? temp_ : Headers_));

                if (!response.IsSuccessStatusCode)
                {
                    attempts++;
                    await login();
                }
                else
                {
                    WriteCookies();
                    result = response;
                    break;
                }
            }

            return(result);
        }
Exemple #2
0
        private async Task <HttpClientResponse> HttpClientRequest(string Method, string Url, dynamic Data = null, Dictionary <string, string> Headers = null)
        {
            var uri = new Uri(Url);
            HttpClientHandler handler = new HttpClientHandler();

            handler.CookieContainer = cookies;

            var response = new HttpClientResponse();
            HttpResponseMessage result = null;

            client             = new HttpClient(handler);
            client.BaseAddress = uri;

            string      dataPostString = JsonConvert.SerializeObject(Data);
            HttpContent dataPost       = new StringContent(dataPostString, Encoding.UTF8, "application/json");

            if (Headers != null && Headers.Count > 0)
            {
                foreach (var header in Headers)
                {
                    if (header.Key.ToLower() == "accept")
                    {
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(header.Value));
                    }
                    else
                    {
                        client.DefaultRequestHeaders.Add(header.Key, header.Value);
                    }
                }
            }

            try
            {
                switch (Method.ToLower())
                {
                case "get":
                    result = await client.GetAsync(Url);

                    break;

                case "post":
                    result = await client.PostAsync(Url, dataPost);

                    break;

                case "put":
                    result = await client.PutAsync(Url, dataPost);

                    break;

                case "delete":
                    result = await client.DeleteAsync(Url);

                    break;

                default:
                    break;
                }

                response.ResponseBody = await result.Content.ReadAsStringAsync();

                response.ResponseCookies     = cookies.GetCookies(uri).Cast <Cookie>();
                response.ResponseHeaders     = result.Headers;
                response.StatusCode          = result.StatusCode.ToString();
                response.IsSuccessStatusCode = result.IsSuccessStatusCode;
                response.Success             = true;
            }
            catch (HttpRequestException e)
            {
                response.Error = e.Message;
            }

            return(response);
        }