Ejemplo n.º 1
0
        private RestResponse ExecuteRestRequest(string resourcePath, HttpMethod httpMethod, string formValues = null)
        {
            if (Log.IsDebugEnabled)
            {
                Log.DebugFormat("REST {0} to {1}", httpMethod, resourcePath);
            }

            HttpWebResponse webResponse;
            try
            {
                HttpWebRequest request = CreateHttpWebRequest(resourcePath, httpMethod, formValues);
                webResponse = (HttpWebResponse) request.GetResponse();
            }
            catch (WebException ex)
            {
                var exResponse = ex.Response as HttpWebResponse;
                if (exResponse != null)
                {
                    Log.Debug("REST invocation returned an error code", ex);
                    webResponse = exResponse;
                }
                else
                {
                    Log.Error("REST invocation threw an unexpected exception", ex);
                    throw new MembaseManagementConnectionException(
                        "An unexpected error occurred trying to connect to the Membase server", ex);
                }
            }

            return new RestResponse(webResponse.StatusCode, GetResponseBodyAsString(webResponse));
        }
Ejemplo n.º 2
0
        private HttpWebRequest CreateHttpWebRequest(string resourcePath, HttpMethod httpMethod, string formValues)
        {
            var request = (HttpWebRequest)WebRequest.Create(CreateResourceUri(resourcePath));
            request.Method = httpMethod.ToString();
            request.Accept = MimeTypes.Json;
            request.UserAgent = UserAgent;

            if (!string.IsNullOrWhiteSpace(_adminUserName))
            {
                request.Credentials = new NetworkCredential(_adminUserName, _adminPassword);
            }

            if (httpMethod == HttpMethod.Post)
            {
                request.ContentType = MimeTypes.FormPost;
                SetFormPostValues(request, formValues);
            }

            return request;
        }