asp.net C# 获取网页源码的几种方式

本文介绍了两种在ASP.NET C#中获取网页源码的常见方法,包括使用WebBrowser控件和HTTP请求的方式,详细阐述了各自的操作步骤和应用场景。

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

1 方法
               System.Net.WebClient aWebClient = new System.Net.WebClient();
               aWebClient.Encoding = System.Text.Encoding.Default;
               Byte[] pageData = aWebClient.DownloadData(url);
           
               string nhtml = Encoding.GetEncoding("utf-8").GetString(pageData);

2方法

                System.Net.WebClient aWebClient = new System.Net.WebClient();
                aWebClient.Encoding = System.Text.Encoding.Default;
                string nhtml = aWebClient.DownloadString(goodstidurl);

3方法
               WebBrowser webbrowser = new WebBrowser();
                StreamReader sr = new StreamReader(this.webBTaobao.DocumentStream, Encoding.Default);
                html = sr.ReadToEnd();
                html = html.Replace("\r\n", "");
                html = html.Replace("\n", "");
                html = html.Replace("  ", "");
                html = html.Replace("(", "");
                html = html.Replace(")", "");
                string nurl = Regex.Match(html, "(?<=data-url=\").*?(?=\")").Value;
                //新建一个WebBrowser
                WebBrowser webAddress = new WebBrowser();
                webAddress.Navigate(nurl);
                //等待加载完成
                while (webAddress.ReadyState < WebBrowserReadyState.Complete) Application.DoEvents();
                StreamReader sraddress = new StreamReader(webAddress.DocumentStream, Encoding.Default);
                jsonaddress = sraddress.ReadToEnd();

4方法

            WebRequest hwr = WebRequest.Create(@"http://item.taobao.com/item.htm?id=" + row["urlId"].ToString());//向指定Url发出请求 
            HttpWebResponse hwp = hwr.GetResponse() as HttpWebResponse;//将hwr对HTTP的请求 
            string text;
            StreamReader sr;
            string code = hwp.ContentType;//请求响应得到的内容类型 
            //得到编码了 
            code = code.Split('=')[1];
            Stream rep = hwp.GetResponseStream();//将请求得到的内容以流的形式读出 
            sr = new StreamReader(rep, Encoding.GetEncoding(code));//用指定的字符编码为指定的流初始化 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值