Cambiar una página por otra Visual Basic

(1/1)

Adrylek:
Me gustaría saber si hay alguna manera para cambiar una página por otra, es decir, al ir a google, que salga youtube (por ejemplo), como lo hace el Fiddler.

Gracias..

Eleкtro:
¿Puedes ser más específico?, ¿Pretendes hacerlo en tiempo de ejecución en un WebBrowser Control?, ¿O pretendes que los cambios sean permanentes en el PC con cualquier navegador?.

Para lo segundo es tán sencillo como añadir una entrada el archivo HOSTS. http://msdn.microsoft.com/en-us/library/ff749174.aspx

EDITO: Y para lo primero puedes checkar la url del control para modificarla por la que quieras.

Código
   Private Sub WebBrowser1_Navigated(sender As Object, e As WebBrowserNavigatedEventArgs) _
   Handles WebBrowser1.Navigated
 
           Select Case True
 
               Case sender.url.ToString.ToLower.StartsWith("http://www.google")
                   sender.navigate("youtube.com")
 
               Case Else
                   ' MsgBox(sender.url.ToString.ToLower)
 
           End Select
 
   End Sub

Saludos

Adrylek:
Cita de: EleKtro H@cker en 10 Agosto 2013, 10:18 am

¿Puedes ser más específico?, ¿Pretendes hacerlo en tiempo de ejecución en un WebBrowser Control?, ¿O pretendes que los cambios sean permanentes en el PC con cualquier navegador?.

Para lo segundo es tán sencillo como añadir una entrada el archivo HOSTS. http://msdn.microsoft.com/en-us/library/ff749174.aspx

EDITO: Y para lo primero puedes checkar la url del control para modificarla por la que quieras.

Código
   Private Sub WebBrowser1_Navigated(sender As Object, e As WebBrowserNavigatedEventArgs) _
   Handles WebBrowser1.Navigated
 
           Select Case True
 
               Case sender.url.ToString.ToLower.StartsWith("http://www.google")
                   sender.navigate("youtube.com")
 
               Case Else
                   ' MsgBox(sender.url.ToString.ToLower)
 
           End Select
 
   End Sub

Saludos



Quiero que los cambios sean permanentes en la PC en cualquier navegador, pero que se quiten al desmarcar la checkbox..

Eleкtro:
Cita de: Adrylek en 10 Agosto 2013, 18:44 pm

Quiero que los cambios sean permanentes en la PC

Cita de: EleKtro H@cker en 10 Agosto 2013, 10:18 am

es tán sencillo como añadir una entrada el archivo HOSTS. http://msdn.microsoft.com/en-us/library/ff749174.aspx

Acabo de codear una Class para manipular el archivo Hosts, puedes agregar un nuevo mapeo, bloquear diréctamente una url, eliminar un mapeo ...sólamente lo he testeado lo necesario.

Aquí tienes ;):

Código
#Region " Hosts Helper "
 
Public Class Hosts_Helper
 
 
   ' [ Hosts Helper ]
   '
   ' // By Elektro H@cker
   '
   ' Examples:
   '
   ' MsgBox(Hosts_Helper.HOSTS_Exists)
   ' Hosts_Helper.Add("www.youtube.com", "231.7.66.33")
   ' Hosts_Helper.Block("www.youtube.com")
   ' MsgBox(Hosts_Helper.IsAdded("www.youtube.com"))
   ' MsgBox(Hosts_Helper.IsBlocked("www.youtube.com"))
   ' Hosts_Helper.Remove("www.youtube.com")
   ' Hosts_Helper.Clean_Hosts_File()
 
 
   Shared ReadOnly HOSTS As String = _
   IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "Drivers\etc\hosts")
 
 
   ''' <summary>
   ''' Adds a new Block mapping into the Hosts file.
   ''' </summary>
   Public Shared Sub Block(ByVal URL As String)
 
       Dim Entry As String = String.Format("::1 {0}", URL)
 
       If HOSTS_Exists() AndAlso IsBlocked(URL) Then
 
           Throw New Exception(String.Format("""{0}"" is already blocked.", URL))
           Exit Sub
 
       ElseIf HOSTS_Exists() AndAlso IsAdded(URL) Then
 
           Remove(URL)
 
       End If
 
       Try
           IO.File.AppendAllText(HOSTS, (Environment.NewLine & Entry), System.Text.Encoding.Default)
       Catch ex As Exception
           Throw New Exception(ex.Message)
       End Try
 
   End Sub
 
 
   ''' <summary>
   ''' Adds a new mapping into Hosts file.
   ''' </summary>
   Public Shared Sub Add(ByVal URL As String, ByVal IP_Address As String)
 
       Dim Entry As String = String.Format("{0} {1}", IP_Address, URL)
 
       If HOSTS_Exists() AndAlso (IsAdded(URL) OrElse IsBlocked(URL)) Then
           Throw New Exception(String.Format("""{0}"" is already mapped.", URL))
           Exit Sub
 
       ElseIf Not Validate_IP(IP_Address) Then
           Throw New Exception(String.Format("""{0}"" is not a valid IP adress.", IP_Address))
           Exit Sub
       End If
 
       Try
           IO.File.AppendAllText(HOSTS, (Environment.NewLine & Entry), System.Text.Encoding.Default)
       Catch ex As Exception
           Throw New Exception(ex.Message)
       End Try
 
   End Sub
 
 
   ''' <summary>
   ''' Removes a blocked or an added URL from the Hosts file.
   ''' </summary>
   Public Shared Sub Remove(ByVal URL As String)
 
       If Not HOSTS_Exists() Then
           Throw New Exception("HOSTS File does not exists.")
           Exit Sub
       ElseIf HOSTS_Exists() And Not (IsAdded(URL) OrElse IsBlocked(URL)) Then
           Throw New Exception(String.Format("""{0}"" is not added yet.", URL))
           Exit Sub
       End If
 
       Try
 
           Dim Content As String = _
               System.Text.RegularExpressions.Regex.Replace(IO.File.ReadAllText(HOSTS).ToLower, _
               String.Format("(\d{{1,3}}\.\d{{1,3}}\.\d{{1,3}}\.\d{{1,3}}|::1)(\s+|\t+){0}", URL.ToLower), String.Empty)
 
           IO.File.WriteAllText(HOSTS, Content, System.Text.Encoding.Default)
 
       Catch ex As Exception
           Throw New Exception(ex.Message)
       End Try
 
   End Sub
 
 
   ''' <summary>
   ''' Checks if an URL is already added into the Hosts file.
   ''' </summary>
   Public Shared Function IsAdded(ByVal URL As String) As Boolean
 
       Return If(Not HOSTS_Exists(), False, _
                 System.Text.RegularExpressions.Regex.IsMatch( _
                 System.Text.RegularExpressions.Regex.Replace(IO.File.ReadAllText(HOSTS).ToLower, "\s+|\t+", ";"), _
                 String.Format(";[^\#]?\d{{1,3}}\.\d{{1,3}}\.\d{{1,3}}\.\d{{1,3}};{0}", URL.ToLower)))
 
   End Function
 
 
   ''' <summary>
   ''' Checks if an URL is already blocked into the Hosts file.
   ''' </summary>
   Public Shared Function IsBlocked(ByVal URL As String) As Boolean
 
       Return If(Not HOSTS_Exists(), False, _
                 System.Text.RegularExpressions.Regex.IsMatch( _
                 System.Text.RegularExpressions.Regex.Replace(IO.File.ReadAllText(HOSTS).ToLower, "\s+|\t+", String.Empty), _
                 String.Format("[^\#](127.0.0.1|::1){0}", URL.ToLower)))
 
   End Function
 
 
   ''' <summary>
   ''' Checks if the Hosts file exists.
   ''' </summary>
   Public Shared Function HOSTS_Exists() As Boolean
       Return IO.File.Exists(HOSTS)
   End Function
 
 
   ''' <summary>
   ''' Cleans all the mappings inside the Hosts file.
   ''' </summary>
   Public Shared Sub Clean_Hosts_File()
       Try
           IO.File.WriteAllText(HOSTS, String.Empty)
       Catch ex As Exception
           MsgBox(ex.Message)
       End Try
   End Sub
 
 
   ' Validates an IP adress.
   Private Shared Function Validate_IP(ByVal IP_Address As String) As Boolean
       Dim IP As System.Net.IPAddress = Nothing
       Return System.Net.IPAddress.TryParse(IP_Address, IP)
   End Function
 
End Class
 
#End Region

De nada  :P

Navegación

[0] Índice de Mensajes