FormatOperationUrlString() публичный статический Метод

public static FormatOperationUrlString ( string hostUrl, string operationPageUrl ) : string
hostUrl string
operationPageUrl string
Результат string
Пример #1
0
        /// <summary>
        /// Responsible of accessing the page and submitting the post. Generic handler for the post access
        /// </summary>
        /// <param name="parameters"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        protected string MakePostRequest(string webPage)
        {
            // Add required stuff to validate the page request
            string postBody = "__REQUESTDIGEST=" + Utilities.ReadHiddenField(webPage, "__REQUESTDIGEST") +
                              "&__EVENTVALIDATION=" + Utilities.ReadHiddenField(webPage, "__EVENTVALIDATION") +
                              "&__VIEWSTATE=" + Utilities.ReadHiddenField(webPage, "__VIEWSTATE");

            // Add operation specific parameters
            foreach (var item in this.PostParameters)
            {
                postBody = postBody + string.Format("&{0}={1}", item.Key, item.Value);
            }

            string results = string.Empty;

            try
            {
                string url = Utilities.FormatOperationUrlString(this.TargetSiteUrl, this.OperationPageUrl);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                if (this.AuthType != AuthenticationType.Office365)
                {
                    // Get X-RequestDigest header for post. Required for most of the operations
                    request.Headers.Add("X-RequestDigest", GetUpdatedFormDigest(this.TargetSiteUrl));
                }

                // Note that this assumes that we use particular identity running the thread
                ModifyRequestBasedOnAuthPattern(request);

                // Set some reasonable limits on resources used by this request
                request.MaximumAutomaticRedirections = 6;
                request.MaximumResponseHeadersLength = 6;
                // Set credentials to use for this request.
                request.ContentType = "application/x-www-form-urlencoded";

                byte[] postByte = Encoding.UTF8.GetBytes(postBody);
                request.ContentLength = postByte.Length;
                Stream postStream = request.GetRequestStream();
                postStream.Write(postByte, 0, postByte.Length);
                postStream.Close();

                HttpWebResponse wResp = (HttpWebResponse)request.GetResponse();
                postStream = wResp.GetResponseStream();
                StreamReader postReader = new StreamReader(postStream);

                results = postReader.ReadToEnd();

                postReader.Close();
            }
            catch (Exception ex)
            {
                // Give better description for the exception
                throw new Exception("MakePostRequest failed.", ex);
            }

            return(results);
        }
Пример #2
0
        /// <summary>
        /// Handles the initial request of the page using given identity
        /// </summary>
        /// <returns></returns>
        protected string GetRequest()
        {
            string         returnString = string.Empty;
            string         url          = Utilities.FormatOperationUrlString(this.TargetSiteUrl, this.OperationPageUrl);
            HttpWebRequest request      = (HttpWebRequest)WebRequest.Create(url);

            //Set auth options based on auth model
            ModifyRequestBasedOnAuthPattern(request);

            // Set some reasonable limits on resources used by this request
            request.MaximumAutomaticRedirections = 6;
            request.MaximumResponseHeadersLength = 6;
            // Set user agent as valid text
            request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

                Stream responseStream = response.GetResponseStream();
                if (response.ContentEncoding.ToLower().Contains("gzip"))
                {
                    responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
                }
                else if (response.ContentEncoding.ToLower().Contains("deflate"))
                {
                    responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
                }

                // Get the response stream and store that as string
                using (StreamReader reader = new StreamReader(responseStream, encode))
                {
                    returnString = reader.ReadToEnd();
                }

                return(returnString);
            }
        }