Ejemplo n.º 1
0
        private static string MakeRestFulApiCall(string url, RestfulMethods requestMethod, string postdata)
        {
            StringBuilder restURL = new StringBuilder();
            try
            {
                restURL.AppendFormat(url);
                HttpWebRequest restRequest = (HttpWebRequest) WebRequest.Create(restURL.ToString());
                restRequest.Method = GetRequestMethod(requestMethod);
                restRequest.Timeout = System.Threading.Timeout.Infinite;
                restRequest.ContentType = "application/json";

                #region For Offshore Connectivity
                string webProxy = AppCommon.GetAppSettingValue("WebProxyUrl");
                if (webProxy != null && !webProxy.Equals(""))
                {
                    WebProxy proxy = new WebProxy {UseDefaultCredentials = true};
                    Uri proxyurl = new Uri(webProxy);
                    proxy.Address = proxyurl;
                    restRequest.Proxy = proxy;
                    restRequest.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
                }
                #endregion

                if (postdata != null)
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(postdata);
                    restRequest.ContentLength = bytes.Length;
                    using (Stream ps = restRequest.GetRequestStream())
                    {
                        ps.Write(bytes, 0, bytes.Length);
                    }
                }
                // restRequest.Credentials = new NetworkCredential("admin", "Frinov25");
                string aptoLogin = AppCommon.GetAppSettingValue("AptoUserId");
                string aptoPassword = AppCommon.GetAppSettingValue("AptoPassword");
                restRequest.Credentials = new NetworkCredential(aptoLogin, aptoPassword);

                HttpWebResponse restResponse = (HttpWebResponse) restRequest.GetResponse();
                ExceptionManager.Info(RestApiRequestString(url,GetRequestMethod(requestMethod),postdata));
                string result;
                using (StreamReader reader = new StreamReader(restResponse.GetResponseStream()))
                {
                    result = reader.ReadToEnd();
                }
                return result;
            }
            catch (WebException webEx)
            {
                StringBuilder errorString = new StringBuilder();
                if (webEx.Status == WebExceptionStatus.ProtocolError)
                {
                    errorString.AppendFormat("Status Code : {0}", ((HttpWebResponse) webEx.Response).StatusCode);
                    errorString.AppendFormat("Status Description : {0}",
                                             ((HttpWebResponse) webEx.Response).StatusDescription);
                }
                ExceptionManager.Error(RestApiRequestString(url,GetRequestMethod(requestMethod),postdata),webEx);
            }
            return "";
        }
Ejemplo n.º 2
0
 private static string GetRequestMethod(RestfulMethods methodType)
 {
     string method;
     switch (methodType)
     {
         case RestfulMethods.Delete:
             method = "DELETE";
             break;
         case RestfulMethods.Post:
             method = "POST";
             break;
         case RestfulMethods.Put:
             method = "PUT";
             break;
         default:
             method = "GET";
             break;
     }
     return method;
 }