在VB中,可以使用WebClient類或HttpWebRequest類來(lái)獲取網(wǎng)頁(yè)數(shù)據(jù)。
使用WebClient類:
Dim webClient As New WebClient()
Dim html As String = webClient.DownloadString("https://www.example.com")
使用HttpWebRequest類:
Dim request As HttpWebRequest = CType(WebRequest.Create("https://www.example.com"), HttpWebRequest)
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
Dim streamReader As New StreamReader(response.GetResponseStream())
Dim html As String = streamReader.ReadToEnd()
response.Close()
streamReader.Close()
注意:在使用上述方法時(shí),需要導(dǎo)入System.Net
和System.IO
命名空間。另外,還需要處理異常情況,例如網(wǎng)絡(luò)連接失敗或網(wǎng)頁(yè)不存在等。