Example #1
0
        public async Task <T> ExecAsync <T>(HttpIORequest request) where T : new()
        {
            string url = request.URI;

            if (request.QueryString != null)
            {
                var builder = new UriBuilder(request.URI);
                builder.Port  = -1; // Use default
                builder.Query = ToQueryString(request.QueryString);
                url           = builder.ToString();
            }
            switch (request.Verb)
            {
            case HttpVerb.GET:
                return(await ExecAsyncGet <T>(request, url).ConfigureAwait(false));
            }
            return(default(T));
        }
Example #2
0
        private async Task <T> ExecAsyncGet <T>(HttpIORequest request, string url) where T : new()
        {
            T deserializedObj = default(T);

            using (var client = new HttpClient())
            {
                HttpResponseMessage response = null;
                client.Timeout = new TimeSpan(0, 0, 9000);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                if (request.Authentication == AuthType.BASIC)
                {
                    string sAuth     = request.BasicAuthCreds.Username + ":" + request.BasicAuthCreds.Password;
                    var    byteArray = Encoding.UTF8.GetBytes(sAuth);
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
                }
                else if (request.Authentication == AuthType.OAUTH2)
                {
                    /*
                     *  To generate oauth2 header following items are needed
                     *  1. Consumer key
                     *  2. Consumer secret
                     *  3. Token (optional)
                     *  4. Token secret (optional)
                     */
                    //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", authHeader);
                }
                try
                {
                    response = await client.GetAsync(url).ConfigureAwait(false);

                    if (response.IsSuccessStatusCode)
                    {
                        string strRspns = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                        deserializedObj = await Task.Run
                                          (
                            () => JsonConvert.DeserializeObject <T>(strRspns)
                                          ).ConfigureAwait(false);
                    }
                    else
                    {
                        if (OnHttpAppException != null)
                        {
                            string msg = "Http Server threw exception";
                            if (response != null)
                            {
                                msg += string.Format(": HTTP {0}\n{1}",
                                                     response.StatusCode, Convert.ToString(response));
                            }
                            OnHttpAppException(msg);
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Most likely Severe System or Networkr related exception
                    if (OnHttpFatalException != null)
                    {
                        string msg = "Http fatal error: " + ex.Message;
                        OnHttpFatalException(msg);
                    }
                    //throw new HttpIOException("Http operation failed with message:" + ex.Message);
                }
            }
            return(deserializedObj);
        }