Exemple #1
0
        public string GetPage(Uri requestUri, string referer, bool allowRedirect, bool checkSessionStatus)
        {
            try
            {
                RequestDefaultUri();
                WebClientRequestManager.WaitWhileMaxSimultaneousRequestIsReached();
                HttpWebRequest httpRequest;
                WebResponse    response;
                Exception      exception         = null;
                bool           submitCredentials = false;

                if (!string.IsNullOrEmpty(_userName) && !string.IsNullOrEmpty(_password))
                {
                    allowRedirect = false;
                }

                do
                {
                    httpRequest = PrepareGetRequest(requestUri, allowRedirect, referer);
                    response    = SendGetRequest(httpRequest, out exception);
                    ParseWebException(response, exception, requestUri, out submitCredentials);
                } while (submitCredentials);

                if (response != null && response.Headers != null && response.Headers["Location"] != null && response.Headers["Location"].Length > 0)
                {
                    do
                    {
                        response.Close();
                        Uri location = GetRedirectLocation(requestUri, response.Headers["Location"].ToString());
                        response    = null;
                        httpRequest = PrepareGetRequest(location, allowRedirect, referer);
                        response    = SendGetRequest(httpRequest, out exception);
                        ParseWebException(response, exception, requestUri, out submitCredentials);
                    } while (submitCredentials);
                }

                return(ReadResponseContent(response, checkSessionStatus));
            }
            catch (Exception ex)
            {
                OnLogWebClientException(requestUri, ex);
                return(Constants.ERROR);
            }
            finally
            {
                WebClientRequestManager.ReleaseMaxRequestLockWait();
            }
        }
Exemple #2
0
        ///// <summary>
        ///// Post data must be included in the requestUri.
        ///// E.g. http://www.domain.com/index.php?page=home&lang=en
        ///// Post data will be extracted and send via POST.
        ///// </summary>
        ///// <param name="requestUri"></param>
        ///// <param name="referer"></param>
        ///// <param name="allowRedirect"></param>
        ///// <param name="checkSessionStatus"></param>
        ///// <returns></returns>
        //public string PostSynchronous(Uri requestUri, string referer, bool allowRedirect, bool checkSessionStatus)
        //{
        //    HttpWebRequest httpRequest = PreparePostRequest(requestUri, allowRedirect, referer);
        //    byte[] postData = Encoding.ASCII.GetBytes(Utilities.GetPostStringFromUri(requestUri));
        //    return ReadResponseContent(SendPostRequest(httpRequest, postData), checkSessionStatus);
        //}

        private WebResponse GetWebResponseViaGetMethod(Uri requestUri, string referer, bool allowRedirect)
        {
            try
            {
                WebClientRequestManager.WaitWhileMaxSimultaneousRequestIsReached();
                WebResponse response;
                Exception   exception         = null;
                bool        submitCredentials = false;

                do
                {
                    HttpWebRequest httpRequest = PrepareGetRequest(requestUri, allowRedirect, referer);
                    response = SendGetRequest(httpRequest, out exception);
                    ParseWebException(response, exception, requestUri, out submitCredentials);
                } while (submitCredentials);

                return(response);
            }
            finally
            {
                WebClientRequestManager.ReleaseMaxRequestLockWait();
            }
        }