Allora, perdonami se non rispondo in privato, ma la bellezza del forum è quella di essere pubblico e utile così a tutti.
Ho fatto solo un esperimento tempo fa. Faccio una classe per recuperare l'html della pagina.
Dato che il recupero e la manipolazione dell'html non è fisso, ma dipende dal contesto, lo faccio fare nella pagina. Perciò dichiaro un delegate.
la classe e il delegate è:
codice:
Public Delegate Function DelegateSetHtml(ByVal html As String) As String
Public Class ResponseFilter
Inherits Stream
Private m_sink As Stream
Private m_position As Long
Private m_function As DelegateSetHtml
Public Sub New(ByVal sink As Stream, ByVal functionHtml As DelegateSetHtml)
Me.m_sink = sink
Me.m_function = functionHtml
End Sub
Public Overrides ReadOnly Property CanRead() As Boolean
Get
Return True
End Get
End Property
Public Overrides ReadOnly Property CanSeek() As Boolean
Get
Return False
End Get
End Property
Public Overrides ReadOnly Property CanWrite() As Boolean
Get
Return False
End Get
End Property
Public Overrides Sub Flush()
Me.m_sink.Flush()
End Sub
Public Overrides ReadOnly Property Length() As Long
Get
Return 0
End Get
End Property
Public Overrides Property Position() As Long
Get
Return Me.m_position
End Get
Set(ByVal Value As Long)
Me.m_position = Value
End Set
End Property
Public Overrides Function Read(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer) As Integer
Return Me.m_sink.Read(buffer, offset, count)
End Function
Public Overrides Function Seek(ByVal offset As Long, ByVal origin As System.IO.SeekOrigin) As Long
Return 0
End Function
Public Overrides Sub SetLength(ByVal value As Long)
Me.m_sink.SetLength(value)
End Sub
Public Overrides Sub Close()
Me.m_sink.Close()
End Sub
Public Overrides Sub Write(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer)
Dim Html As String = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count)
Html = Me.m_function(Html)
buffer = System.Text.UTF8Encoding.UTF8.GetBytes(Html)
Me.m_sink.Write(buffer, 0, buffer.Length)
End Sub
End Class
Una qualunque pagina fa:
codice:
Option Strict On
Partial Class CorsoApogeo_leggere_il_contenuto_html_della_pagina_intercettare_html
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Inserire qui il codice utente necessario per inizializzare la pagina
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Response.Filter = New ResponseFilter(Response.Filter, AddressOf modificaHtml)
End Sub
Public Function modificaHtml(ByVal html As String) As String
Dim FileName As String = "c:\tmp\response.htm"
Try
html = html.Replace("PIETRO", "----NICOLA---")
File.WriteAllText(FileName, html, Encoding.UTF8)
Catch ex As Exception
html &= "<script>alert('Errore nella scrittura del file');</script>"
End Try
Return html
End Function
Public Function modificaHtml_old(ByVal html As String) As String
Dim fs As FileStream = Nothing
Dim buffer As Byte()
Try
fs = New FileStream("c:\tmp\response.htm", FileMode.OpenOrCreate, FileAccess.Write)
html = html.Replace("PIETRO", "----NICOLA---")
buffer = System.Text.UTF8Encoding.UTF8.GetBytes(html)
fs.Write(buffer, 0, buffer.Length)
Catch ex As Exception
html &= "<script>alert('Errore nella scrittura del file');</script>"
Finally
If Not (fs Is Nothing) Then fs.Close()
End Try
Return html
End Function
End Class
qui ho fatto una semplice sostituzione, ma è ovvio che con le espressioni regolari non vedo limiti all'orizzonte.