前台代码:
- <asp:Panel ID="Panel1" runat="server">
- 网址:<asp:TextBox ID="txtUrl" runat="server"></asp:TextBox>
- <asp:Button ID="btnGet" runat="server" Text="RSS" OnClick="btnGet_Click" />
- </asp:Panel>
后台代码:
- using System.Text;//因为用了Encoding类
- using System.Net; //因为用了WebClient 类
- using System.Net;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void btnGet_Click(object sender, EventArgs e)
- {
- string strurl = txtUrl.Text.ToString(); //欲获取的网页地址 要 http://
- WebClient myWebClient = new WebClient(); //创建WebClient实例myWebClient
- //获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
- myWebClient.Credentials = CredentialCache.DefaultCredentials;
- //从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号)
- byte[] pagedata = myWebClient.DownloadData(@strurl);
- //string result = Encoding.Default.GetString(pagedata); //如果获取网站页面采用的是GB2312,则使用这句
- string result = Encoding.UTF8.GetString(pagedata); //如果获取网站页面采用的是UTF-8,则使用这句
- Response.Write(result); //在WEB页中显示获取的内容
- Panel1.Visible = false;
- }
- }
本文介绍了一个简单的ASP.NET Web应用程序,该程序通过用户输入的网址抓取RSS内容,并将其显示在Web页面上。主要使用了WebClient类来下载指定URL的数据,并根据网页编码选择合适的编码方式解析内容。
514

被折叠的 条评论
为什么被折叠?



