1. Scaricare una pagina web libera
Function GetWebPage(ByVal Url As String) As String
Dim request As WebRequest = WebRequest.Create(Url)
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
Dim dataStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim testo As String = reader.ReadToEnd()
reader.Close()
dataStream.Close()
response.Close()
Return testo
End Function
2. Comunicazione con una pagina con passaggio di cookie
Es. con un comando postare user e password per effettuare il logon, poi accedere usando i cookie di sessione per accedere al contenuto protetto.
Public Class http_data
Public response_text As String
Public cookyCollection As ArrayList
End Class
Public Function postingCommands(ByVal comando As String, ByVal cookieIn As ArrayList) As http_data
UrlDovePostare as string=”Esempio di url”
Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(comandoPSI)
Dim request As System.Net.HttpWebRequest = System.Net.WebRequest.Create(UrlDovePostare)
request.Credentials = System.Net.CredentialCache.DefaultCredentials
CType(request, System.Net.HttpWebRequest).UserAgent = “Nome agente utente”
request.Method = “POST”
request.ContentLength = byteArray.Length
request.ContentType = “application/x-www-form-urlencoded”
request.CookieContainer = New System.Net.CookieContainer()
Dim cook As System.Net.Cookie
If Not cookieIn Is Nothing Then
For Each cook In cookieIn
request.CookieContainer.Add(cook)
Next
End If
Dim dataStream As System.IO.Stream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim response As System.Net.HttpWebResponse = request.GetResponse()
Dim statoDesc As String = CType(response, System.Net.HttpWebResponse).StatusDescription
dataStream = response.GetResponseStream()
Dim reader As New System.IO.StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
Dim risp As New http_data
risp.response_text = responseFromServer
risp.cookyCollection = New ArrayList
For Each cook In response.Cookies
risp.cookyCollection.Add(cook)
Next
reader.Close()
dataStream.Close()
response.Close()
Return risp
End Function
2b. Come sopra, senza cookie per postare dei dati come se fosse un form
Public Shared Function SalvaWeb(Nome as string, Cognome as string) As String
UrlDovePostare=”Esempio di pagina”
Dim data As String = “”
With anag
data &= “&nome=” & HttpUtility.UrlEncode(Nome)
data &= “&cognome=” & HttpUtility.UrlEncode(Cognome)
End With
Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(data)
Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(UrlDovePostare)
request.Credentials = System.Net.CredentialCache.DefaultCredentials
CType(request, System.Net.HttpWebRequest).UserAgent = “NOME USER AGENT”
request.Method = “POST”
request.ContentLength = byteArray.Length
request.ContentType = “application/x-www-form-urlencoded”
Dim dataStream As System.IO.Stream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim response As System.Net.WebResponse = request.GetResponse()
Dim statoDesc As String = CType(response, System.Net.HttpWebResponse).StatusDescription
dataStream = response.GetResponseStream()
Dim reader As New System.IO.StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
reader.Close()
dataStream.Close()
response.Close()
Return responseFromServer
End Function