in

DotNetSide

Dot Net South Italy Developers User Group

Tips

Leggere il sorgente di una pagina html

Autore: Vito Arconzo

Ecco una funzione che restituisce il codice HTML di una pagina web.
 

1        Function GetHtmlPageSource(ByVal url As String, _
2                Optional ByVal username As String = Nothing, _
3                Optional ByVal password As String = Nothing) As String
4            Dim st As System.IO.Stream
5            Dim sr As System.IO.StreamReader
6    
7            Try
8                ' invia una Web request
9                Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(url)
10   
11               ' se sono specificati username/password usa le credenziali
12               If Not username Is Nothing AndAlso Not password Is Nothing Then
13                   req.Credentials = New System.Net.NetworkCredential(username, password)
14               End If
15   
16               ' ricava la risposta e legge lo stream coi risultati
17               Dim resp As System.Net.WebResponse = req.GetResponse
18               st = resp.GetResponseStream
19               sr = New System.IO.StreamReader(st)
20   
21               Return sr.ReadToEnd
22           Catch ex As Exception
23               Return ""
24           Finally
25               sr.Close()
26               st.Close()
27           End Try
28       End Function
 
Only published comments... Jun 30 2006, 11:55 AM by VitoA
Filed under:
Powered by Community Server (Commercial Edition), by Telligent Systems