| |
Atlas快速入门之实战Atlas |
|
时间: 2006-09-21 来自:天极yesky |
 |
|
首先,由于要处理XML,读取网站上的资源,所以要引入如下几个命名空间
Imports System.Net Imports System.IO Imports System.Xml | 当用户点选某个分类主题的按钮时,将会自动下载该网站中符合主题的RSS XML文件,下载完后再进行解析,再显示到页面中去。代码如下:
Public Function SendRequest( _ ByVal URI As String, _ ByVal requestType As String) As HttpWebRequest Dim req As HttpWebRequest = Nothing Try //发起HTTP请求 req = HttpWebRequest.Create(URI) req.Method = requestType Catch ex As Exception Throw New Exception("Error") End Try Return req End Function
Public Function GetResponse( _ ByVal req As HttpWebRequest) As String Dim body As String = String.Empty Try //从服务器中获得响应输出流 Dim resp As HttpWebResponse = req.GetResponse() Dim stream As Stream = resp.GetResponseStream()
//使用streamreader去处理得到的服务器响应 Dim reader As StreamReader = _ New StreamReader(stream, Encoding.UTF8) body = reader.ReadToEnd() stream.Close() Catch ex As Exception Throw New Exception("Error") End Try Return body End Function | 上面的两个方法,分别向网站服务器发出获取指定URL的信息的请求,并用IO流中的streamreader的方式获得服务器返回的输出信息。而下面的LoadRSS()方法,传入的参数是指定的URL地址,然后分别调用上面的两个方法,在最后获得服务器返回的输出流后,再通过解析XML的方式,选择合适的结点内容进行返回。代码如下:
Public Function LoadRSS( _ ByVal URI As String) As String Dim req As HttpWebRequest Dim xmlDoc As XmlDocument = Nothing Try XmlDataSource1.DataFile = URI
//下载RSS XML文件 req = SendRequest(URI, "GET") Dim xmlData As String = GetResponse(req) xmlDoc = New XmlDocument() xmlDoc.LoadXml(xmlData)
//选择合适的结点 Dim titleNode As XmlNode = _ xmlDoc.DocumentElement.SelectSingleNode("channel/title")
Return titleNode.InnerText Catch ex As Exception Return String.Empty End Try End Function | 接着,我们还有为10多个按钮编写响应的事件,当点选了某个分类主题的按钮后,则向服务器请求指定的分类主题的RSS XML,代码如下:
Public Sub Button_Click( _ ByVal sender As Object, ByVal e As System.EventArgs) _ Handles Button1.Click, Button2.Click, Button3.Click, _ Button4.Click, Button5.Click, Button6.Click, _ Button7.Click, Button8.Click, Button9.Click, _ Button10.Click, Button11.Click
Dim URL As String = String.Empty Select Case CType(sender, Button).Text Case "Database" : URL = _ "http://services.devx.com/outgoing/databasefeed.xml" Case ".NET" : URL = _ "http://services.devx.com/outgoing/dotnet.xml" Case "C++" : URL = _ "http://services.devx.com/outgoing/cplusfeed.xml" Case "Recent Tips" : URL = _ "http://services.devx.com/outgoing/recentTipsFeed.xml" Case "Web Dev" : URL = _ "http://services.devx.com/outgoing/webdevfeed.xml" Case "Latest" : URL = _ "http://services.devx.com/outgoing/devxfeed.xml" Case "Enterprise" : URL = _ "http://services.devx.com/outgoing/enterprisefeed.xml" Case "Wireless / Mobile" : URL = _ "http://services.devx.com/outgoing/wirelessfeed.xml" Case "XML" : URL = _ "http://services.devx.com/outgoing/xmlfeed.xml" Case "Java" : URL = _ "http://services.devx.com/outgoing/javafeed.xml" Case "Open Source" : URL = _ "http://services.devx.com/outgoing/openSourceFeed.xml" End Select Label1.Text = LoadRSS(URL) End Sub | 最后,在LOAD事件中,默认读取最新信息的RSS XML
Protected Sub Page_Load( _ ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Label1.Text = _ LoadRSS("http://services.devx.com/outgoing/devxfeed.xml") End Sub | 运行后效果如下图所示,点选左边每个分类主题的按钮,都会自动去该网站下载最新的RSS XML 并且进行解析,最后展示到页面中去,而这一切都由于用了Atlas而是无刷新的。
|
|
|
|
|
|
标签NewsAbout错误:Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
|
|
|
|
|
|