取得網頁資料的某部份
有時候我們想要取得某一個網頁的某一個部份的值
.NET裡面就有很好用的Class {System.Net} 裡的 HttpWebRequest
所以寫個簡單範例來看一下
Function GetPage(ByVal txtURL As String) As String
Dim strResponse As String
Dim strState As String
Dim strServer As String
Dim strTitle As String
Dim Result As String
Dim Request As HttpWebRequest
Dim Response As HttpWebResponse
Request = WebRequest.Create(txtURL)
Response = Request.GetResponse
Dim Answer As Stream
Dim SR As StreamReader
Answer = Response.GetResponseStream
SR = New StreamReader(Answer, Text.Encoding.UTF8)
strResponse = SR.ReadToEnd()
strState = Response.StatusCode.ToString
strServer = Response.Server
SR.Close()
If strState = "OK" Then
If strResponse.Length > 0 And strResponse.IndexOf("<title>") > 0 Then
strTitle = strResponse.Substring(strResponse.IndexOf("<title>") + 7, strResponse.IndexOf("</title>") - strResponse.IndexOf("<title>") - 7)
Result = strServer & "-" & strState & "-" & strTitle
Else
Result = strServer & "-" & strState & "-" & " XXXX "
End If
Else
Result = strServer & "-" & strState & "-" & " XXXX "
End If
Return Result
End Function
Protected Sub BTN_Search_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTN_Search.Click
LAB_Result.Text = GetPage(TB_URL.Text)
End Sub
結果顯示 -- WebServer名稱, 狀態, 網頁的Title
2009年6月1日 星期一
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言