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

本文介绍了两种不同的方法来获取网页的内容:一种是使用webrequest,另一种是利用更具体的HttpWebRequest。通过这两种方法,可以有效地从指定URL中读取并返回整个网页的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

        }

 

转载于:https://www.cnblogs.com/neru/archive/2011/03/24/1993837.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值