using System;
using System.Collections.Generic;
using System.Text;

namespace iNethinkCMS.Helper
{
    class Common
    {
        /// <summary>
        /// Web GET方式提交
        /// </summary>
        /// <param name="url"></param>
        /// <param name="_Encoding">字符编码</param>
        /// <returns></returns>
        public static string HttpWebRequestGET(string url, Encoding _Encoding = null, bool isUrlDecode = true, string _ContentType = null)
        {
            try
            {
                System.GC.Collect();
                if (_Encoding == null)
                {
                    _Encoding = Encoding.Default;
                }

                if (System.Net.ServicePointManager.DefaultConnectionLimit < 512)
                {
                    System.Net.ServicePointManager.DefaultConnectionLimit = 512;
                }


                System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
                webRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/json,*/*";
                webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Zune 4.7; BOIE9;ZHCN)";
                webRequest.Method = "GET";
                webRequest.Timeout = 600000;
                webRequest.KeepAlive = false;
                webRequest.ProtocolVersion = System.Net.HttpVersion.Version10;
                if (_ContentType != null)
                {
                    webRequest.ContentType = _ContentType;
                }
                System.Threading.Thread.Sleep(5);
                using (System.Net.WebResponse wr = webRequest.GetResponse())
                {
                    System.IO.StreamReader str = new System.IO.StreamReader(wr.GetResponseStream(), _Encoding);
                    string strMsg = str.ReadToEnd();
                    str.Close();
                    str.Dispose();
                    str = null;
                    webRequest.Abort();
                    webRequest = null;
                    if (isUrlDecode)
                    {
                        strMsg = System.Web.HttpUtility.UrlDecode(strMsg);
                    }
                    return strMsg;
                    //在这里对接收到的页面内容进行处理  
                }

            }
            catch (Exception ex)
            {

                System.Net.ServicePointManager.Expect100Continue = false;
                return "Request failed,Exception:" + ex.Message;


            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}