在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
命名空间。另外,还需要处理异常情况,例如网络连接失败或网页不存在等。