91 lines
4.7 KiB
VB.net
91 lines
4.7 KiB
VB.net
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) As String
|
|
Return EjecutaURL(fullUrl, System.Text.Encoding.UTF8, bAllowAutoRedirect, iTimeout)
|
|
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("¡", "?").Replace("#", "%23")
|
|
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
|