Ejemplo n.º 1
0
        protected async Task <T> InvokeAsync <T>(string operationUrl, Constants.HttpMethod method, List <KeyValue> metaData = null)
        {
            StringBuilder requestUrl = new StringBuilder(string.Concat(this.ApiRoot, operationUrl, "?"));

            if (metaData != null)
            {
                foreach (var k in metaData)
                {
                    requestUrl.Append(string.Concat(k.Key, "=", k.Value));
                    requestUrl.Append("&");
                }
            }

            var request = WebRequest.Create(requestUrl.ToString());


            if (method != Constants.HttpMethod.GET)
            {
                request.ContentLength = 0;
            }



            return
                (await
                 this.GetAsync <T>(method.ToString(), request));
        }
        private async Task <S> InvokeAsync <T, S>(dynamic imageRequest, string operationUrl, Constants.HttpMethod method, List <KeyValue> metaData)
        {
            StringBuilder requestUrl = new StringBuilder(string.Concat(_apiRoot, operationUrl, "?"));

            foreach (var k in metaData)
            {
                requestUrl.Append(string.Concat(k.Key, "=", k.Value));
                requestUrl.Append("&");
            }
            var request = WebRequest.Create(requestUrl.ToString());

            return
                (await
                 this.SendAsync <T, S>(method.ToString(), imageRequest, request)
                 .ConfigureAwait(false));
        }
Ejemplo n.º 3
0
        protected async Task <TS> InvokeAsync <T, TS>(dynamic imageRequest, string operationUrl, Constants.HttpMethod method, List <KeyValue> metaData = null)
        {
            StringBuilder requestUrl = new StringBuilder(string.Concat(this.ApiRoot, operationUrl, "?"));

            if (metaData != null)
            {
                foreach (var k in metaData)
                {
                    requestUrl.Append(string.Concat(k.Key, "=", k.Value));
                    requestUrl.Append("&");
                }
            }
            var request = WebRequest.Create(requestUrl.ToString());

            return
                (await
                 this.SendAsync <T, TS>(method.ToString(), imageRequest, request));
        }
Ejemplo n.º 4
0
        private async Task <S> InvokeAsync <T, S>(dynamic imageRequest, string operationUrl, Constants.HttpMethod method, List <KeyValue> metaData)
        {
            //Get Token
            var token = await GetToken();

            StringBuilder requestUrl = new StringBuilder(string.Concat(this.ApiRoot, operationUrl, "?"));

            foreach (var k in metaData)
            {
                requestUrl.Append(string.Concat(k.Key, "=", k.Value));
                requestUrl.Append("&");
            }
            requestUrl.Append(string.Concat("subscription-key=", this.SubscriptionKey, "&"));
            var request = WebRequest.Create(requestUrl.ToString());

            request.Headers.Add("Authorization", string.Concat("Bearer ", token.access_token));

            return
                (await
                 this.SendAsync <T, S>(method.ToString(), imageRequest, request)
                 .ConfigureAwait(false));
        }
Ejemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="method"></param>
        /// <param name="content"></param>
        /// <param name="headList"></param>
        /// <param name="paramList"></param>
        /// <param name="isNeedErrorReturn"></param>
        /// <returns></returns>
        public static async Task <string> getHttpWebRequest(string uri, Constants.HttpMethod method = Constants.HttpMethod.GET, string content = "", List <KeyValuePair <string, string> > headList = null, List <KeyValuePair <string, string> > paramList = null, bool isNeedErrorReturn = false)
        {
            HttpResponseMessage response = null;
            string responseReturn        = "";

            return(await Task.Run(() =>
            {
                if (IsNetworkAvailable())
                {
                    HttpClient httpClient = new HttpClient();
                    if (headList != null && headList.Count != 0)
                    {
                        foreach (var head in headList)
                        {
                            httpClient.DefaultRequestHeaders.Add(head.Key, head.Value);
                        }
                    }
                    HttpRequestMessage requst;
                    switch (method.ToString())
                    {
                    case "POST":
                        if (paramList == null || paramList.Count == 0)
                        {
                            response = httpClient.PostAsync(new Uri(uri), new StringContent(content, Encoding.UTF8, "application/json")).Result;
                        }
                        else
                        {
                            response = httpClient.PostAsync(new Uri(uri), new System.Net.Http.FormUrlEncodedContent(paramList)).Result;
                        }
                        break;

                    case "GET":
                        response = httpClient.GetAsync(new Uri(uri)).Result;
                        break;

                    case "PUT":
                        response = httpClient.PutAsync(new Uri(uri), new StringContent(content, Encoding.UTF8, "application/json")).Result;
                        break;

                    case "DELETE":
                        requst = new HttpRequestMessage(HttpMethod.Delete, new Uri(uri));
                        requst.Content = new StringContent(content, Encoding.UTF8, "application/json");
                        response = httpClient.SendAsync(requst).Result;
                        break;

                    case "HEAD":
                        requst = new HttpRequestMessage(HttpMethod.Head, new Uri(uri));
                        requst.Content = new StringContent(content, Encoding.UTF8, "application/json");
                        response = httpClient.SendAsync(requst).Result;
                        break;

                    case "OPTIONS":
                        requst = new HttpRequestMessage(HttpMethod.Options, new Uri(uri));
                        requst.Content = new StringContent(content, Encoding.UTF8, "application/json");
                        response = httpClient.SendAsync(requst).Result;
                        break;

                    case "TRACE":
                        requst = new HttpRequestMessage(HttpMethod.Trace, new Uri(uri));
                        requst.Content = new StringContent(content, Encoding.UTF8, "application/json");
                        response = httpClient.SendAsync(requst).Result;
                        break;

                    default:
                        break;
                    }
                    if (response.StatusCode == HttpStatusCode.OK || isNeedErrorReturn)
                    {
                        responseReturn = response.Content.ReadAsStringAsync().Result;
                    }
                }
                else
                {
                    throw new NetWorkErrorException();
                }
                return responseReturn;
            }));
        }