在VB中,可以使用WebClient類或HttpWebRequest類來獲取網頁數據。
使用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()
注意:在使用上述方法時,需要導入System.Net
和System.IO
命名空間。另外,還需要處理異常情況,例如網絡連接失敗或網頁不存在等。