Пример #1
0
        private static Exception ParseFirstError(WebException wex)
        {
            Logger.Debug("Attempting to get and parse the first error returned from forum JSON result...");
            try
            {
                string jsonErrors = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd();
                Logger.Debug("JSON result was: " + jsonErrors);
                wex.Response.Dispose();
                dynamic json = JsonConvert.DeserializeObject(jsonErrors);
                if (json == null)
                {
                    Logger.Debug("JSON error was null.");
                    return(wex);
                }

                return(new InvalidOperationException(json.errors[0].ToString(), wex));
            }
            catch
            {
                Logger.Debug("There was an error attempting to parse the first JSON error.");
                var ex = new InvalidOperationException("Unknown error connecting to the forum API. Ensure the host name is correct in web.config. The sidebar category must also exist on NodeBB.", wex);
                ForumHelper.PauseConnections(ex, 10);
                return(ex);
            }
        }
Пример #2
0
        private string PutOrPostRequest(string relativeUrl, IEnumerable <KeyValuePair <string, string> > postData, string method)
        {
            lock (requestLock)
            {
                string requestUrl = this.GetRequestUrl(relativeUrl);

                var request = WebRequest.Create(requestUrl);
                request.Method      = method;
                request.Timeout     = Config.NodeBB.ApiRequestTimeout;
                request.ContentType = "application/x-www-form-urlencoded";

                Logger.Debug("Sending forum {0} request to URL: {1}", method, request.RequestUri);

                try
                {
                    using (var body = request.GetRequestStream())
                        using (var writer = new StreamWriter(body))
                        {
                            foreach (var pair in postData)
                            {
                                writer.Write("&{0}={1}", HttpUtility.UrlEncode(pair.Key), HttpUtility.UrlEncode(pair.Value));
                            }
                        }

                    using (var response = request.GetResponse())
                        using (var stream = response.GetResponseStream())
                        {
                            Logger.Debug("Response received, response code was: {0}", GetResponseCode(response));
                            return(new StreamReader(stream).ReadToEnd());
                        }
                }
                catch (TimeoutException tex)
                {
                    Logger.Debug("Timeout exception for {0} request to URL: {1}", method, request.RequestUri);
                    ForumHelper.PauseConnections(tex, 10);
                    throw;
                }
                catch (WebException wex)
                {
                    Logger.Debug("Web exception for {0} request to URL: {1}", method, request.RequestUri);
                    ForumHelper.PauseConnections(wex, 10);
                    throw ParseFirstError(wex);
                }
            }
        }
Пример #3
0
        private string GetOrDeleteRequest(string method, string urlFormat, params object[] args)
        {
            lock (requestLock)
            {
                string relativeUrl = string.Format(urlFormat, args);

                string requestUrl = this.GetRequestUrl(relativeUrl);

                var request = WebRequest.Create(requestUrl);
                request.Method  = method;
                request.Timeout = Config.NodeBB.ApiRequestTimeout;

                Logger.Debug("Sending forum {0} request to URL: {1}", method, request.RequestUri);

                try
                {
                    using (var response = request.GetResponse())
                        using (var stream = response.GetResponseStream())
                        {
                            Logger.Debug("Response received, response code was: {0}", GetResponseCode(response));
                            return(new StreamReader(stream).ReadToEnd());
                        }
                }
                catch (TimeoutException tex)
                {
                    Logger.Debug("Timeout exception for {0} request to URL: {1}", method, request.RequestUri);
                    ForumHelper.PauseConnections(tex, 10);
                    throw;
                }
                catch (WebException wex)
                {
                    Logger.Debug("Web exception for {0} request to URL: {1}", method, request.RequestUri);
                    ForumHelper.PauseConnections(wex, 10);
                    throw ParseFirstError(wex);
                }
            }
        }