示例#1
0
        public static CookieManager Instance()
        {
            if (instance == null)
            {
                instance = new CookieManager();
            }

            return instance;
        }
示例#2
0
        /// <summary>
        /// Retrieves the content of a specified URL in a response to a request.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="logIn"></param>
        /// <returns></returns>
        private string RetrieveFromUrl(HttpWebRequest request, bool logIn, string charset)
        {
            StreamReader reader = null;

            string strContent = string.Empty;

            HttpWebResponse response = null;

            Stream responseStream = null;


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

                //Accept SSL certificate automatically
                System.Net.ServicePointManager.ServerCertificateValidationCallback =
                    delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
                {
                    // Replace this line with code to validate server certificate.
                    return(true);
                };

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

                // 2. Get the Stream Object from the response
                responseStream = response.GetResponseStream();

                CookieManager.AddNewCookieContainer(_userID, response.Cookies);

                // 3. Create a stream reader and associate it with the stream object
                //reader = new StreamReader(responseStream, Encoding.GetEncoding(string.IsNullOrEmpty(response.CharacterSet) ? "utf-8" : response.CharacterSet));
                reader = new StreamReader(responseStream, Encoding.GetEncoding(string.IsNullOrEmpty(charset) ? "utf-8" : charset));

                if (!string.IsNullOrEmpty(response.CharacterSet))
                {
                    this._charset = response.CharacterSet;
                }

                // 4. read the entire stream
                strContent = reader.ReadToEnd();

#if DEBUG
                watch.Stop();
                MyLogger.Write("END " + request.RequestUri + " " + watch.ElapsedMilliseconds.ToString(), "RetriveFromUrl", LoggingCategory.Debug);
#endif
                charset = response.CharacterSet;

                return(strContent);
            }
            catch (Exception ex)
            {
                MyLogger.WriteWithStack(ex + "; " + request.RequestUri.OriginalString, "RetriveFromUrl", LoggingCategory.Exception);

                throw;
            }
            finally
            {
                if (response != null)
                {
                    if (request.RequestUri.OriginalString.Contains("id=-1&ctype=xml"))
                    {
                        MyLogger.Write(response.Server, "RetriveFromUrl", LoggingCategory.Debug);
                    }

                    response.Close();
                }

                if (responseStream != null)
                {
                    responseStream.Close();
                }

                if (reader != null)
                {
                    reader.Close();
                }
            }
        }