Imports System.IO

Public Class http
    Public Shared Function EjecutaURL(ByVal fullUrl As String,
        Optional ByVal bAllowAutoRedirect As Boolean = True,
        Optional ByVal iTimeout As Integer = 120000, Optional Codificacion As System.Text.Encoding = Nothing) As String
        If Codificacion Is Nothing Then
            Return EjecutaURL(fullUrl, System.Text.Encoding.UTF8, bAllowAutoRedirect, iTimeout)
        Else
            Return EjecutaURL(fullUrl, Codificacion, bAllowAutoRedirect, iTimeout)
        End If
    End Function
    Public Shared Function EjecutaURL(ByVal fullUrl As String, _
                                      ByVal Codificacion As System.Text.Encoding, _
        Optional ByVal bAllowAutoRedirect As Boolean = True, _
        Optional ByVal iTimeout As Integer = 120000) As String
        Dim webRequest As System.Net.HttpWebRequest = Nothing
        Dim webResponse As System.Net.HttpWebResponse = Nothing
        Try
            'Creamos un HttpWebRequest con la URL especificada.
            webRequest = CType(System.Net.WebRequest.Create(fullUrl), System.Net.HttpWebRequest)
            webRequest.AllowAutoRedirect = bAllowAutoRedirect
            'webRequest.MaximumAutomaticRedirections = 50
            webRequest.Timeout = iTimeout


            'Enviamos la peticion y esperamos una respuesta.
            Try
                webResponse = CType(webRequest.GetResponse(), System.Net.HttpWebResponse)
                Select Case (webResponse.StatusCode)
                    Case System.Net.HttpStatusCode.OK
                        'Leemos el contenido de la respuesta
                        Dim responseStream As System.IO.Stream = _
                            webResponse.GetResponseStream()
                        '   Dim responseEncoding As System.text.Encoding = _
                        'System.text.Encoding.Default
                        'Mandamos la respuesta a un stream reader con su codificacion correspondiente 
                        Dim responseReader As New StreamReader(responseStream, Codificacion)
                        Dim responseContent As String = _
                            responseReader.ReadToEnd()
                        Return responseContent.Trim
                    Case System.Net.HttpStatusCode.Redirect, System.Net.HttpStatusCode.MovedPermanently
                        Throw New System.Exception(String.Format( _
                            "No ha sido posible leer el contenido de la respuesta. La URL ha sido movida." & _
                            " StatusCode={0}.", webResponse.StatusCode))
                    Case System.Net.HttpStatusCode.NotFound
                        Throw New System.Exception(String.Format( _
                            "No ha sido posible leer el contenido de la respuesta. La URL no se encuentra." & _
                            " StatusCode={0}.", webResponse.StatusCode))
                    Case Else
                        Throw New System.Exception(String.Format( _
                            "No ha sido posible leer el contenido de la respuesta. StatusCode={0}.", _
                            webResponse.StatusCode))
                End Select
            Catch we As System.Net.WebException
                If (we.Status = Net.WebExceptionStatus.Timeout) Then
                    'Return False
                    Throw New System.Exception("No ha sido posible ejecutar la URL (TIMEOUT).", we)
                End If
                Throw New System.Exception("No ha sido posible ejecutar la URL (webException).", we)
            Finally
                If (Not IsNothing(webResponse)) Then
                    webResponse.Close()
                End If
            End Try
        Catch e As System.Exception
            Throw New System.Exception("No ha sido posible ejecutar la URL (systemException).", e)
        End Try
    End Function

    Public Shared Function EjemacHP(ByVal UrlCGIBIN As String, ByVal Macro As String, Optional ByVal Parametros As String = "", Optional Codificacion As System.Text.Encoding = Nothing) As String
        Try
            Dim sUrl As String
            If Parametros <> "" Then
                Parametros = Parametros.Replace("¡", "?")
                sUrl = UrlCGIBIN & "?" & Macro & "=" & Parametros
            Else
                sUrl = UrlCGIBIN & "?" & Macro
                'sUrl = UrlCGIBIN & "?" & Macro & "=" & Parametros & "?" & Macro
            End If
            ' Dim sRespuesta As String = EjecutaURL(sUrl, System.Text.Encoding.Default, , 500000)
            If Codificacion Is Nothing Then
                Dim sRespuesta As String = EjecutaURL(sUrl, System.Text.Encoding.Default, , 500000)
                Return sRespuesta
            Else
                Dim sRespuesta As String = EjecutaURL(sUrl, Codificacion, , 500000)
                Return sRespuesta
            End If

        Catch EX As Exception
            Throw New Exception(EX.Message, EX)
        End Try
    End Function
End Class
