Esempio n. 1
0
        public HgResumeApiResponse Execute(string method, HgResumeApiParameters parameters, byte[] contentToSend, int secondsBeforeTimeout)
        {
            string queryString = parameters.BuildQueryString();

            Url = string.Format("{0}://{1}/api/v{2}/{3}?{4}", _url.Scheme, _url.Host, ApiVersion, method, queryString);
            var req = (HttpWebRequest)WebRequest.Create(Url);

            req.UserAgent       = $"HgResume v{ApiVersion}";
            req.PreAuthenticate = true;
            if (string.IsNullOrEmpty(Properties.Settings.Default.LanguageForgeUser) ||
                string.IsNullOrEmpty(ServerSettingsModel.PasswordForSession))
            {
                throw new HgResumeException("Missing username or password");
            }
            req.Credentials = new NetworkCredential(Properties.Settings.Default.LanguageForgeUser, ServerSettingsModel.PasswordForSession);
            req.Timeout     = secondsBeforeTimeout * 1000;         // timeout is in milliseconds
            if (contentToSend.Length == 0)
            {
                req.Method = WebRequestMethods.Http.Get;
            }
            else
            {
                req.Method        = WebRequestMethods.Http.Post;
                req.ContentLength = contentToSend.Length;
                req.ContentType   = "text/plain";                // i'm not sure this is really what we want.  The other possibility is "application/x-www-form-urlencoded"
                using (var reqStream = req.GetRequestStream())
                {
                    reqStream.Write(contentToSend, 0, contentToSend.Length);
                }
            }


            HttpWebResponse     res;
            HgResumeApiResponse apiResponse;
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            try
            {
                using (res = (HttpWebResponse)req.GetResponse())
                {
                    apiResponse = HandleResponse(res);
                }
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    using (res = (HttpWebResponse)e.Response)
                    {
                        apiResponse = HandleResponse(res);
                    }
                }
                else if (e.Status == WebExceptionStatus.Timeout)
                {
                    apiResponse = null;
                }
                else
                {
                    throw;                     // throw for other types of network errors (see WebExceptionStatus for the full list of errors)
                }
            }
            finally
            {
                stopwatch.Stop();
            }
            if (apiResponse != null)
            {
                apiResponse.ResponseTimeInMilliseconds = stopwatch.ElapsedMilliseconds;
            }
            return(apiResponse);
        }
Esempio n. 2
0
        public HgResumeApiResponse Execute(string method, HgResumeApiParameters parameters, byte[] contentToSend, int secondsBeforeTimeout)
        {
            string queryString = parameters.BuildQueryString();

            _urlExecuted = String.Format("{0}://{1}/api/v{2}/{3}?{4}", _url.Scheme, _url.Host, APIVERSION, method, queryString);
            var req = WebRequest.Create(_urlExecuted) as HttpWebRequest;

            req.UserAgent       = String.Format("HgResume v{0}", APIVERSION);
            req.PreAuthenticate = true;
            if (!_url.UserInfo.Contains(":"))
            {
                throw new HgResumeException("Username or password were not supplied in custom location");
            }
            req.Credentials = new NetworkCredential(UserName, Password);
            req.Timeout     = secondsBeforeTimeout * 1000;         // timeout is in milliseconds
            if (contentToSend.Length == 0)
            {
                req.Method = WebRequestMethods.Http.Get;
            }
            else
            {
                req.Method        = WebRequestMethods.Http.Post;
                req.ContentLength = contentToSend.Length;
                req.ContentType   = "text/plain";                // i'm not sure this is really what we want.  The other possibility is "application/x-www-form-urlencoded"
                using (var reqStream = req.GetRequestStream())
                {
                    reqStream.Write(contentToSend, 0, contentToSend.Length);
                }
            }


            HttpWebResponse     res;
            HgResumeApiResponse apiResponse;
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            try
            {
                using (res = (HttpWebResponse)req.GetResponse())
                {
                    apiResponse = HandleResponse(res);
                }
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    using (res = (HttpWebResponse)e.Response)
                    {
                        apiResponse = HandleResponse(res);
                    }
                }
                else if (e.Status == WebExceptionStatus.Timeout)
                {
                    apiResponse = null;
                }
                else
                {
                    throw;                     // throw for other types of network errors (see WebExceptionStatus for the full list of errors)
                }
            }
            finally
            {
                stopwatch.Stop();
            }
            if (apiResponse != null)
            {
                apiResponse.ResponseTimeInMilliseconds = stopwatch.ElapsedMilliseconds;
            }
            return(apiResponse);
        }
        public HgResumeApiResponse Execute(string method, HgResumeApiParameters parameters, byte[] contentToSend, int secondsBeforeTimeout)
        {
            string queryString = parameters.BuildQueryString();
            _urlExecuted = String.Format("{0}://{1}/api/v{2}/{3}?{4}", _url.Scheme, _url.Host, APIVERSION, method, queryString);
            var req = WebRequest.Create(_urlExecuted) as HttpWebRequest;
            req.UserAgent = String.Format("HgResume v{0}", APIVERSION);
            req.PreAuthenticate = true;
            if (!_url.UserInfo.Contains(":"))
            {
                throw new HgResumeException("Username or password were not supplied in custom location");
            }
            req.Credentials = new NetworkCredential(UserName, Password);
            req.Timeout = secondsBeforeTimeout * 1000; // timeout is in milliseconds
            if (contentToSend.Length == 0)
            {
                req.Method = WebRequestMethods.Http.Get;
            }
            else
            {
                req.Method = WebRequestMethods.Http.Post;
                req.ContentLength = contentToSend.Length;
                req.ContentType = "text/plain";  // i'm not sure this is really what we want.  The other possibility is "application/x-www-form-urlencoded"
                using (var reqStream = req.GetRequestStream())
                {
                    reqStream.Write(contentToSend, 0, contentToSend.Length);
                }
            }

            HttpWebResponse res;
            HgResumeApiResponse apiResponse;
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            try
            {
                using (res = (HttpWebResponse)req.GetResponse())
                {
                    apiResponse = HandleResponse(res);
                }

            }
            catch(WebException e)
            {
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    using (res = (HttpWebResponse)e.Response)
                    {
                        apiResponse = HandleResponse(res);
                    }
                }
                else if (e.Status == WebExceptionStatus.Timeout)
                {
                    apiResponse = null;
                }
                else
                {
                    throw; // throw for other types of network errors (see WebExceptionStatus for the full list of errors)
                }
            }
            finally
            {
                stopwatch.Stop();
            }
            if (apiResponse != null)
            {
                apiResponse.ResponseTimeInMilliseconds = stopwatch.ElapsedMilliseconds;
            }
            return apiResponse;
        }