Example #1
0
        /// <summary>
        /// Forces a POST of data to a specified URL.
        /// A HTTP POST is a combination of a write to the Web Server
        /// and an immediate read from the Web server.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <param name="logIn"></param>
        /// <returns></returns>
        public string PostToUrl(string url, string data, bool logIn)
        {
            string username = String.Empty;
            string password = String.Empty;

            url = HttpHelper.GetHTTPAuthenticationInfo(url, out username, out password);

            // Create the Web Request Object
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

            // Specify that you want to POST data
            request.Method      = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8");

            //httpauthentication
            byte[] credentialBuffer = new UTF8Encoding().GetBytes(
                username + ":" +
                password);
            request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer);

            CookieContainer cc = new CookieContainer();

            CookieCollection cCollection = cookieManager.GetCookieCollectionByUserId(_userID);

            if (cCollection != null)
            {
                cc.Add(cCollection);
            }
            else
            {
                cc.Add(new CookieCollection());
            }

            request.CookieContainer = cc;

            this.LogCookieCollection(cc.GetCookies(request.RequestUri));

            if (url != null)
            {
                // write out the data to the web server
                WriteToUrl(request, data);
            }
            else
            {
                request.ContentLength = 0;
            }

            // read the response from the Web Server
            string htmlContent = RetrieveFromUrl(request, logIn, this._charset);

            return(htmlContent);
        }
Example #2
0
        /// <summary>
        /// Determines if we have a file stream set, and returns either
        /// the HttpWebRequest stream of the file.
        /// </summary>
        /// <returns></returns>
        public virtual System.IO.Stream GetStream( )
        {
            System.IO.Stream io;
            if (null == coFileStream)
            {
                CookieContainer cc = new CookieContainer();

                CookieManager cookieManager = CookieManager.Instance();

                CookieCollection cCollection = cookieManager.GetCookieCollectionByUserId(this._connectionID);

                if (cCollection != null)
                {
                    cc.Add(cCollection);
                }
                else
                {
                    cc.Add(new CookieCollection());
                }


                coRequest.CookieContainer = cc;

                io = coRequest.GetRequestStream();
            }
            else
            {
                io = coFileStream;
            }
            return(io);
        }