/// <summary>
        /// Web Request Wrapper
        /// </summary>
        /// <param name="method">Http Method</param>
        /// <param name="url">Full url to the web resource</param>
        /// <param name="postData">Data to post in querystring format</param>
        /// <param name="headers">Additional Header Data</param>
        /// <returns>The web server response.</returns>
        public string WebRequest(Enums.Method method, string url, string postData, List <KeyValuePair <string, string> > headers, string contentType)
        {
            HttpWebRequest webRequest    = null;
            StreamWriter   requestWriter = null;
            string         responseData  = "";

            webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
            if (headers != null)
            {
                foreach (KeyValuePair <string, string> header in headers)
                {
                    webRequest.Headers.Add(header.Key, header.Value);
                }
            }

            webRequest.Method = method.ToString();
            webRequest.ServicePoint.Expect100Continue = false;

            if (method == Enums.Method.POST || method == Enums.Method.DELETE)
            {
                webRequest.ContentType = contentType;
                //POST the data.
                using (requestWriter = new StreamWriter(webRequest.GetRequestStream()))
                {
                    requestWriter.Write(postData);
                    requestWriter.Close();
                }
            }

            responseData = WebResponseGet(webRequest);
            webRequest   = null;

            return(responseData);
        }
Beispiel #2
0
        private void ResponseVerifications(BaseRestSharp.IRestResponse response, bool throwException, string url, Enums.Method methodType)
        {
            // end of index logic
            if (throwException)
            {
                if (response.ErrorException != null)
                {
                    throw response.ErrorException;
                }
            }

            if (!IsRequestStatusCodeSuccess(response.StatusCode))
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.NotFound:
                    throw new WebApiException($"{url} has thrown status code : {response.StatusCode}. Please check that you have spelled the url correctly");

                case HttpStatusCode.InternalServerError:
                    throw new WebApiException($"{url} has thrown status code : {response.StatusCode}.{response.ErrorMessage} Content :{response.Content}");

                case HttpStatusCode.MethodNotAllowed:
                    throw new WebApiException($"{url} has thrown status code : {response.StatusCode}. {methodType} verb is not supported. Change the method type to a supported one");

                default:
                    throw new WebApiException($"{url} has thrown status code : {response.StatusCode} {response.ErrorMessage} {response.Content}");
                }
            }
        }
Beispiel #3
0
        public static string ConsumirAPIConToken(Enums.Method method, string request, string BaseURL, string servicePrefix, string controller, string Token = "")
        {
            try
            {
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                string response;
                var    ruta = string.Format("{0}{1}{2}", BaseURL, servicePrefix, controller);
                using (var client = new WebClient())
                {
                    client.Encoding = System.Text.Encoding.UTF8;

                    client.Headers[HttpRequestHeader.ContentType] = "application/json; charset=utf-8";

                    if (Token != "")
                    {
                        client.Headers[HttpRequestHeader.Authorization] = "Bearer " + Token;
                    }
                    if (method == Enums.Method.POST)
                    {
                        response = client.UploadString(ruta, method.ToString(), request);
                    }
                    else
                    {
                        response = client.DownloadString(ruta);
                    }

                    /// Y LA 'RETORNAMOS'
                    return(response);
                }
            }
            catch (WebException ex)
            {
                var excepcion = (HttpWebResponse)ex.Response;
                if (excepcion != null && excepcion.StatusCode.Equals(HttpStatusCode.Unauthorized))
                {
                    string respuesta;
                    try
                    {
                        var ruta = string.Format("{0}{1}{2}", BaseURL, servicePrefix, controller);

                        using (var client = new WebClient())
                        {
                            client.Encoding = System.Text.Encoding.UTF8;
                            client.Headers[HttpRequestHeader.ContentType]   = "application/json; charset=utf-8";
                            client.Headers[HttpRequestHeader.Authorization] = "Bearer " + GetToken();
                            if (method == Enums.Method.POST)
                            {
                                respuesta = client.UploadString(ruta, method.ToString(), request);
                            }
                            else
                            {
                                respuesta = client.DownloadString(ruta);
                            }

                            /// Y LA 'RETORNAMOS'
                            return(respuesta);
                        }
                    }
                    catch (Exception ex2) { }
                }
                return(null);
            }
            catch
            {
                return(null);
            }
        }