多语言展示
当前在线:1186今日阅读:193今日分享:47

使用C#/APS.NET/.NET编写代理,获取HTML源代码

使用C#/APS.NET/.NET编写HttpWebRequest加代理模式获取HTML源代码,两种模式一种是不使用代理模式,一种使用代理模式获取HTML源代码。
方法/步骤
1

#region 根据连接获取源文件        ///

        /// 根据连接获取源文件        ///         /// URL地址        /// IP地址,列如:81.210.118.81        /// port端口,列如:8080        /// html源文件        public string GetContent(Uri URL, string Address, string port)        {            try            {                WebProxy proxyObject = new WebProxy(Address, Convert.ToInt32(port));                CookieContainer Cc = new CookieContainer();                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);                request.Proxy = proxyObject;                request.Timeout = 2500;                request.Method = 'Get';                request.KeepAlive = false;                request.CookieContainer = Cc;                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())                {                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding('GB2312')))                        return reader.ReadToEnd();                }            }            catch (Exception)            {                return 'Error';            }            finally            { }        }        #endregion

2

public string GetContent(Uri url)        {            try            {                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);                request.Timeout = 5000;                HttpWebResponse response = (HttpWebResponse)request.GetResponse();                System.IO.StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding('gb2312'));                string content = sr.ReadToEnd();                sr.Close();                response.Close();                return content;            }            catch (Exception)            {                return 'Error';            }        }

推荐信息