예제 #1
0
        /// <summary>
        /// Retrieves the contents of a specified URL in response to a request.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="logIn"></param>
        /// <returns></returns>
        public string GetFromUrl(string url, bool logIn)
        {
            string username = String.Empty;
            string password = String.Empty;

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

#if DEBUG
            MyLogger.Write(String.Format("Start HttpGet from {0}", url), "GetFromUrl", LoggingCategory.General);
#endif

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

            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();

            string htmlContent = string.Empty;

            try
            {
                CookieCollection cCollection = cookieManager.GetCookieCollectionByUserId(_userID);

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

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

                request.CookieContainer = cc;

                htmlContent = RetrieveFromUrl(request, logIn, this._charset);
            }
            catch (Exception ex)
            {
                MyLogger.Write(ex, "GetFromUrl", LoggingCategory.Exception);
                throw ex;
            }
            finally {
#if DEBUG
                MyLogger.Write(String.Format("Complete HttpGet from {0}", url), "GetFromUrl", LoggingCategory.General);
#endif
            }

            return(htmlContent);
        }
예제 #2
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);
        }
예제 #3
0
        /// <summary>
        /// Retrieves the contents of a specified URL in response to a request.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="logIn"></param>
        /// <returns></returns>
        public string GetFromUrl(string url, bool doLogOn, int timeout)
        {
            string username = String.Empty;
            string password = String.Empty;

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

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

            request.Timeout = timeout;

            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();

            string htmlContent = string.Empty;

            try
            {
                CookieCollection cCollection = cookieManager.GetCookieCollectionByUserId(_userID);

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

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

                request.CookieContainer = cc;

                htmlContent = RetrieveFromUrl(request, doLogOn, this._charset);
            }
            catch { }

            return(htmlContent);
        }
예제 #4
0
        /// <summary>
        /// Transmits a file to the web server stated in the
        /// URL property. You may call this several times and it
        /// will use the values previously set for fields and URL.
        /// </summary>
        /// <param name="aFilename">The full path of file being
        ///transfered.</param>
        public void SendFile(string fileName)
        {
            System.IO.Stream io = null;

            try
            {
                // The live of this object is only good during
                // this function. Used mainly to avoid passing
                // around parameters to other functions.

                string username = String.Empty;
                string password = String.Empty;

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

                coRequest = (HttpWebRequest)WebRequest.Create(Url);

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

                // Set use HTTP 1.0 or 1.1.
                coRequest.ProtocolVersion = TransferHttpVersion;

                coRequest.Method = "POST";

                coRequest.ContentType = "multipart/form-data; boundary=" + BeginBoundary;

                coRequest.Headers.Add("Cache-Control", "no-cache");

                coRequest.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8");

                coRequest.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";

                coRequest.KeepAlive = true;

                string strFields = GetFormFields();

                string strFileHdr = GetFileHeader(fileName);

                string strFileTlr = GetFileTrailer();

                if (!String.IsNullOrEmpty(fileName))
                {
                    FileInfo info = new FileInfo(fileName);

                    coRequest.ContentLength = strFields.Length + strFileHdr.Length + strFileTlr.Length + info.Length;
                }
                else
                {
                    coRequest.ContentLength = strFields.Length + strFileHdr.Length + strFileTlr.Length;
                }



                io = GetStream();

                WriteString(io, strFields);

                WriteString(io, strFileHdr);

                if (!String.IsNullOrEmpty(fileName))
                {
                    this.WriteFile(io, fileName);
                }

                WriteString(io, strFileTlr);

                GetResponse();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (io != null)
                {
                    io.Close();
                }

                // End the life time of this request object.
                coRequest = null;
            }
        }
예제 #5
0
        /// <summary>
        /// It is a regulary POST, without a response.
        /// Used for optimizing code.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <param name="logIn"></param>
        public HttpWebResponse PostToUrlWhenLogOn(string url, string data, bool logIn)
        {
            HttpWebResponse response = null;

            try
            {
#if DEBUG
                System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
#endif

                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;q=0.7,*;q=0.7");
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

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

                CookieContainer cc = new CookieContainer();

                cc.Add(new CookieCollection());

                request.CookieContainer = cc;

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

                // Get the Web Response Object from the request
                response = (HttpWebResponse)request.GetResponse();

#if DEBUG
                watch.Stop();

                MyLogger.Write(request.RequestUri + " took " + watch.ElapsedMilliseconds.ToString(), "PostToUrlWhenLogOn", LoggingCategory.Debug);
#endif

                return(response);
            }
            catch (Exception ex)
            {
                MyLogger.Write(ex.Message, "PostToUrlWhenLogOn", LoggingCategory.Exception);
                throw ex;
            }
        }