Exemplo n.º 1
0
        private HttpStatusCode HttpPost(string additionalUri, string body, out string responseString)
        {
            OAuth2Client.AccessToken ac = _oauth2Client.GetAccessToken(false);
            if (ac == null)
            {
                responseString = null;
                return(HttpStatusCode.BadRequest);
            }

            responseString = string.Empty;
            HttpStatusCode responseCode = HttpStatusCode.BadRequest;

            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _oauth2Client.GetAccessToken(false).access_token);

            // Build up the data to POST.
            StringContent content = new StringContent(body);

            content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

            try
            {
                HttpResponseMessage message = _httpClient.PostAsync(additionalUri, content).ConfigureAwait(false).GetAwaiter().GetResult();
                responseCode   = message.StatusCode;
                responseString = message.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
                if (!message.IsSuccessStatusCode)
                {
                    Console.WriteLine($"Post fail {message.StatusCode} {responseString}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return(responseCode);
        }
Exemplo n.º 2
0
        private HttpStatusCode HttpGet(string additionalUri, KeyValuePair <string, string>[] parameters, out string responseString)
        {
            OAuth2Client.AccessToken ac = null;

            if (_oauth2Client != null)
            {
                ac = _oauth2Client.GetAccessToken(false);
                if (ac == null)
                {
                    responseString = null;
                    return(HttpStatusCode.BadRequest);
                }
                // We want the response to be JSON.
                _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ac.access_token);
            }
            responseString = string.Empty;

            if (parameters != null)
            {
                StringBuilder sb             = new StringBuilder();
                string[]      parameterPairs = parameters.Select(p => $"{p.Key}={p.Value}").ToArray();
                additionalUri = $"{additionalUri}?{String.Join("&", parameterPairs)}";
            }

            HttpResponseMessage message = null;

            try
            {
                message = _httpClient.GetAsync(additionalUri).ConfigureAwait(false).GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                Trace.WriteLine($"Request Get fail: {_httpClient.BaseAddress}{additionalUri} {ex.Message}");
                return(HttpStatusCode.BadRequest);
            }

            if (message.IsSuccessStatusCode)
            {
                try
                {
                    responseString = message.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
                    return(message.StatusCode);
                }
                catch { }
            }
            return(HttpStatusCode.BadRequest);
        }