文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>用asp.net(c#)获取网页源代码(两种方法)

用asp.net(c#)获取网页源代码(两种方法)

时间:2011-03-24  来源:小奈鲁

http://blog.sina.com.cn/s/blog_6e8dfbfa0100o5ec.html

方法一: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;

        }

 

相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载