c # winfrom download page source

The first:

/// <summary>
/// 下载网页源代码
/// </summary>
/// <param name="Url">网页路径</param>
/// <returns></returns>
private string DownloadCode(string Url)
{
try
{
WebClient webClient = new WebClient();
Byte[] pageData = webClient.DownloadData(Url);
return Encoding.GetEncoding("utf-8").GetString(pageData);
}
catch (Exception ec)
{
throw new Exception(ec.Message.ToString());
}

 


 

 

The second:

 

        ///  <Summary> 
        /// request address, return html code
         ///  </ Summary> 
        ///  <param name = "URL"> address </ param> 
        ///  <param name = "timeOut"> Settings request timeout, 1000 = 1 s </ param> 
        ///  <Returns> </ Returns> 
        public  static  String HttpGet ( String URL, int timeOut) 
        { 
            the try 
            { 
                ServicePointManager.SecurityProtocol = (SecurityProtocolType) 192 | (SecurityProtocolType) 768 | (SecurityProtocolType) 3072 ;
                String HTML; 
                the HttpWebRequest Web_Request = (the HttpWebRequest) the WebRequest.Create (URL); 

                Web_Request.AllowAutoRedirect = to false ;
                 // set the request time 
                Web_Request.Timeout = timeOut;
                 // request mode the GET, the POST 
                Web_Request.Method = " the GET " ;
                 / / identity request 
                Web_Request.UserAgent = " Mozilla / 5.0 (Windows NT 6.1; Win64; x64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 66.0.3359.181 Safari / 537.36 " ;
                // addition request header 
                Web_Request.Headers.Add ( " the Accept-Encoding " , " the gzip, the deflate " ); 
                Web_Request.ContentType = " file application / X-WWW-form-urlencoded " ;
                 // Web_Request.Credentials to CredentialCache.DefaultCredentials = ; 

                // set broker properties WebProxy ------------------------------------------- ------
                 // the WebProxy the WebProxy new new proxy = ( "111.13.7.120", 80);
                 // in the HTTP request prior to initiating the proxy assigned to the Proxy property HttpWebRequest
                 // Web_Request.Proxy = proxy;

                HttpWebResponse Web_Response = (HttpWebResponse)Web_Request.GetResponse();

                if (Web_Response.ContentEncoding.ToLower() == "gzip")  // 如果使用了GZip则先解压
                {
                    using (Stream Stream_Receive = Web_Response.GetResponseStream())
                    {
                        using (var Zip_Stream = new GZipStream(Stream_Receive, CompressionMode.Decompress))
                        {
                            using (StreamReader Stream_Reader = new StreamReader(Zip_Stream, Encoding.Default))
                            {
                                html = Stream_Reader.ReadToEnd();
                            }
                        }
                    }
                }
                else
                {
                    using (Stream Stream_Receive = Web_Response.GetResponseStream())
                    {
                        using (StreamReader Stream_Reader = new StreamReader(Stream_Receive, Encoding.Default))
                        {
                            html = Stream_Reader.ReadToEnd();
                        }
                    }
                }

                return html;
            }
            catch (Exception)
            {
                return "error";
            }

        }

 

Guess you like

Origin www.cnblogs.com/yinmu/p/10994088.html