Пример #1
0
        public static string Get_HTTP(string Url, int TimeOut, string Encoding = "utf-8") // 获取HTTP,返回:网页内容,参数:URL、超时时间(毫秒)、编码(默认utf-8)
        {
            NewWebClient myWebClient = new NewWebClient(TimeOut);
            Stream       myStream    = myWebClient.OpenRead(Url);
            StreamReader sr          = new StreamReader(myStream, System.Text.Encoding.GetEncoding(Encoding));
            string       strHTML     = sr.ReadToEnd();

            myStream.Close();
            return(strHTML);
        }
Пример #2
0
        /// <summary>
        /// 获取HTTP
        /// </summary>
        /// <param name="URL">URL</param>
        /// <param name="Type">提交类型,默认:true(GET),false为POST</param>
        /// <param name="PostDate">POST 数据</param>
        /// <param name="TimeOut">超时时间,默认:30000 毫秒,单位:毫秒</param>
        /// <param name="ReadWriteTimeout">GetResponse 和 GetRequestStream 方法的超时时间,默认:300000 毫秒,单位:毫秒</param>
        /// <param name="CookieContainer">Cookie,默认:null</param>
        /// <param name="UserAgent">UserAgent,默认:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36</param>
        /// <param name="Referer">Referer,默认:null</param>
        /// <param name="Encoding">编码,默认:utf-8</param>
        /// <param name="AllowAutoRedirect">是否重定向,默认:true</param>
        /// <returns>成功返回网页内容,失败返回 null</returns>
        public static string Get_HTTP(string URL, bool Type = true, string PostDate = null, int TimeOut = 30000, int ReadWriteTimeout = 300000, CookieContainer CookieContainer = null, string UserAgent = null, string Referer = null, System.Text.Encoding Encoding = null, bool AllowAutoRedirect = true)
        {
            NewWebClient myWebClient = new NewWebClient();

            if (TimeOut > 100)
            {
                myWebClient.TimeOut = TimeOut;
            }
            if (ReadWriteTimeout > 100)
            {
                myWebClient.ReadWriteTimeout = ReadWriteTimeout;
            }
            myWebClient.CookieContainer = CookieContainer;
            if (UserAgent != null)
            {
                myWebClient.UserAgent = UserAgent;
            }
            myWebClient.Referer           = Referer;
            myWebClient.AllowAutoRedirect = AllowAutoRedirect;
            if (Encoding == null)
            {
                myWebClient.Encoding = System.Text.Encoding.UTF8;
            }
            if (Type == false)
            {
                if (PostDate != null)
                {
                    return(myWebClient.UploadString(URL, PostDate));
                }
                return(null);
            }
            else
            {
                Stream       myStream = myWebClient.OpenRead(URL);
                StreamReader sr       = new StreamReader(myStream);
                string       strHTML  = sr.ReadToEnd();
                myStream.Close();
                return(strHTML);
            }
        }