本站为一个PHP个人门户网站,提供一些代码技术文章、提交免费空间消息、一些代码、让学习更轻松-PHP-小戴

asp.net如何获取网页源代码

  •  asp.net如何获取网页源代码下面将提供几种方法,大家可以去试试。。。。。

    方法一:webrequest

     private string GetStringByUrl(string strUrl)
        {
            WebRequest wrt = WebRequest.Create(strUrl);
            WebResponse wrse = wrt.GetResponse();
            Stream strM = wrse.GetResponseStream();
            StreamReader SR = new StreamReader(strM, Encoding.GetEncoding("gb2312"));
            string strallstrm = SR.ReadToEnd();
            return strallstrm;
        }

    方法二:HttpWebRequest

     public static string GetPage(string url, Encoding encoding)

            {

                HttpWebRequest request
    = null;

                HttpWebResponse response
    = null;

                StreamReader reader
    = null;

               
    try

                {

                    request
    = (HttpWebRequest)WebRequest.Create(url);

                    request.UserAgent
    = "www.svnhost.cn";

                    request.Timeout
    = 20000;

    request.AllowAutoRedirect
    = false;



                    response
    = (HttpWebResponse)request.GetResponse();

                   
    if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)

                    {

                        reader
    = new StreamReader(response.GetResponseStream(), encoding);

                       
    string html = reader.ReadToEnd();



                       
    return html;

                    }

                }

               
    catch

                {

                }

               
    finally

                {

                   
    if (response != null)

                    {

                        response.Close();

                        response
    = null;

                    }
                   
    if (reader != null)

                        reader.Close();

                   
    if (request != null)

                        request
    = null;

                }

              
    return string.Empty;

            }


  • [时间:2008-11-30 13:43:27][点击:]

  • 上下篇文章加载中...
本站关键字:宁波网络公司/宁波网站建设公司/宁波网站制作公司/宁波做网站公司