private string CallAPI_Private_WithParam(string url, NameValueCollection nvc)
        {
            var requestMessage = BuildHttpRequestMessage(url, nvc);
            var response       = httpClient.SendAsync(requestMessage).Result;

            return(response.Content.ReadAsStringAsync().Result);
        }
Example #2
0
        private string CallAPI_NoParam(string url, HttpMethod httpMethod)
        {
            var requestMessage = new HttpRequestMessage(httpMethod, new Uri(url));

            requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JWT_NoParameter());
            var response = httpClient.SendAsync(requestMessage).Result;
            var contents = response.Content.ReadAsStringAsync().Result;

            return(contents);
        }
Example #3
0
        public async Task <T> MakeHttpRequest <T>(string httpUrl)
        {
            if (httpUrl == null)
            {
                throw new ArgumentNullException("httpUrl");
            }

            HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Get, httpUrl);

            HttpResponseMessage response = null;

            try
            {
                response = await MyHttpClient.SendAsync(message);
            }
            catch (Exception ex)
            {
                throw new ApiException("MyHttpClient has faild", ex, ApiExceptionType.InvalidServerResponse);
            }



            if (response == null)
            {
                throw new ApiException("The server did not return a response.", ApiExceptionType.NoServerResponse);
            }

            if (response.IsSuccessStatusCode)
            {
                if (response.Content == null)
                {
                    throw new ApiException("The server returned an empty response.", ApiExceptionType.NoServerResponse);
                }

                var responseJson = await response.Content.ReadAsStringAsync();

                if (String.IsNullOrEmpty(responseJson))
                {
                    throw new ApiException("The server returned an empty response.", ApiExceptionType.NoServerResponse);
                }

                object responseObject;
                try
                {
                    responseObject = JsonConvert.DeserializeObject <T>(responseJson);
                }
                catch (Exception ex)
                {
                    throw new ApiException("The server did not return a response in the expected format.", ex, ApiExceptionType.InvalidServerResponse);
                }

                //cast
                return((T)responseObject);
            }
            else
            {
                var errorMsg = String.Format("The server returned status code {0} with error {1}",
                                             response.StatusCode, response.ReasonPhrase);

                throw new ApiException(errorMsg, ApiExceptionType.ServerError);
            }

            return(default(T));
        }