internal string GetConnectionStringFromSharePointList(string reportServerUrl, ICredentials credentials)
        {
            //assumption that the connection string to the database is stored as an encrypted value in a list called "Configuration"
            string webRequestUrl =
                string.Format("{0}/_api/lists/getbytitle('Configuration')/items/?$select=Title,Value&$filter=startswith(Title,'ConnectionString')", reportServerUrl);

            var webPermission = new System.Net.WebPermission(NetworkAccess.Connect, webRequestUrl);

            webPermission.Assert();


            var endpointRequest = (HttpWebRequest)HttpWebRequest.Create(webRequestUrl);

            endpointRequest.Method      = "GET";
            endpointRequest.Accept      = "application/json;odata=verbose";
            endpointRequest.Credentials = credentials;

            var endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();

            try
            {
                WebResponse webResponse = endpointRequest.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream())
                {
                    using (StreamReader responseReader = new StreamReader(webStream))
                    {
                        try
                        {
                            string response         = responseReader.ReadToEnd();
                            var    connectionString = ExtractConnectionStringFromJSON(response);

                            return(connectionString);
                        }
                        finally
                        {
                            responseReader.Close();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new InvalidOperationException("Error when: GetFromSharePointConfigurationList:", e);
            }
        }
Esempio n. 2
0
        //status code initial value set to -1
        //You should never get that value.
        //It should be 200 for succes
        //204 for succes but empty response
        //any other return code is an error
        //408 is a timeout error.
        public Tuple<String, int> Web_Read(string url, int timeout)
        {
            int statusCode = -1;
            string result = "";
            StreamReader reader = null;

            try
            {
                //asking for permission to call the specified url.
                var permission = new WebPermission();
                permission.AddPermission(NetworkAccess.Connect, url);
                permission.Assert();

                WebRequest request = WebRequest.Create(url);
                request.Timeout = (timeout == 0) ? request.Timeout : timeout;
                WebResponse response = request.GetResponse();

                Stream grs = response.GetResponseStream();
                if (grs != null)
                {
                    reader = new StreamReader(grs);
                    result = reader.ReadToEnd();
                }
                //if the response is empty it will officially return a 204 status code.
                //This is according to the http specification.
                if (result.Length < 1)
                {
                    result = "error";
                    statusCode = 204;
                }
                else
                {
                    //response code 200: HTTP OK request was made succesfully.
                    statusCode = 200;
                }
            }
            catch (WebException ex)
            {
                var resp = (HttpWebResponse)ex.Response;
                if (resp == null) statusCode = 500;
                else
                {
                    //Will parse all .net known http status codes.
                    int.TryParse(resp.StatusCode.ToString(), out statusCode);
                }
                result = "error";
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                if (Debugger.IsAttached) Debugger.Break();
            }
            finally
            {
                // general cleanup
                if (reader != null)
                {
                    reader.Close(); //closes the reader and the response stream it was working on at the same time.
                }
            }

            return Tuple.Create(result, statusCode);
        }
Esempio n. 3
0
        public Tuple<String, int> Web_Read(string url)
        {
            int statusCode = 200;
            string result = "";
            StreamReader reader = null;

            try
            {
                //asking for permission to call the specified url.
                var permission = new WebPermission();
                permission.AddPermission(NetworkAccess.Connect, url);
                permission.Assert();

                WebRequest request = WebRequest.Create(url);
                WebResponse response = request.GetResponse();

                Stream grs = response.GetResponseStream();
                if (grs != null)
                {
                    reader = new StreamReader(grs);
                    result = reader.ReadToEnd();
                }
            }
            catch (WebException ex)
            {
                //Properly handling http errors here
                if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
                {
                    var resp = (HttpWebResponse) ex.Response;
                    switch (resp.StatusCode)
                    {
                        case HttpStatusCode.NotFound:
                            result = "eror";
                            statusCode = 404;
                            break;
                        case HttpStatusCode.Forbidden:
                            result = "error";
                            statusCode = 403;
                            break;
                        case HttpStatusCode.InternalServerError:
                            result = "error";
                            statusCode = 500;
                            break;
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                if (Debugger.IsAttached) Debugger.Break();
            }
            finally
            {
                // general cleanup
                if (reader != null)
                {
                    reader.Close(); //closes the reader and the response stream it was working on at the same time.
                }
            }

            return Tuple.Create(result, statusCode);
        }
Esempio n. 4
0
        public Tuple<String, int> Web_Read(string url)
        {
            int statusCode = 200;
            string result = "";
            WebRequest request = null;
            WebResponse response = null;
            StreamReader reader = null;

            try
            {
                //asking for permission to call the specified url.
                WebPermission permission = new WebPermission();
                permission.AddPermission(NetworkAccess.Connect, url);
                permission.Assert();

                request = WebRequest.Create(url);
                response = request.GetResponse();

                reader = new StreamReader(response.GetResponseStream());
                result = reader.ReadToEnd();
            }
            catch (WebException ex)
            {
                //Properly handling http errors here
                if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
                {
                    var resp = (HttpWebResponse)ex.Response;
                    switch (resp.StatusCode)
                    {
                        case HttpStatusCode.NotFound:
                            result = "eror";
                            statusCode = 404;
                            break;
                        case HttpStatusCode.Forbidden:
                            result = "error";
                            statusCode = 403;
                            break;
                        case HttpStatusCode.InternalServerError:
                            result = "error";
                            statusCode = 500;
                            break;
                    }
                }
            }
            catch (Exception)
            {
                //placeholder for other sorts of exceptions
            }
            finally
            {
                // general cleanup
                if (reader != null)
                {
                    reader.Close(); //closes the reader and the response stream it was working on at the same time.
                }
                if (request != null)
                {
                    request = null; //not really needed but general cleanup
                }
            }

            return Tuple.Create(result, statusCode);
        }