All pastes #385855 Raw Edit

cheerio

public text v1 · immutable
·published 2007-03-08 03:45 UTC
rendered paste body

   Console.WriteLine(EasyHttp.Send("http://www.servbox.net/")) 'dynamic_ip/update.php", "user=cheerio&ip=127.0.0.1", EasyHttp.HTTPMethod.HTTP_GET))
   Console.ReadKey()



the module

Option Strict Off
Imports System.Net
Imports System.io
' EasyHttp.vb class file
Public Class EasyHttp
    Public Enum HTTPMethod As Short
        HTTP_GET = 0
        HTTP_POST = 1
    End Enum

    Public Shared Function Send(ByVal URL As String, _
        Optional ByVal PostData As String = "", _
        Optional ByVal Method As HTTPMethod = HTTPMethod.HTTP_GET, _
        Optional ByVal ContentType As String = "") As String

        Dim Request As HttpWebRequest = WebRequest.Create(URL)
        Dim Response As HttpWebResponse
        Dim SW As StreamWriter
        Dim SR As StreamReader
        Dim ResponseData As String

        ' Prepare Request Object
        Request.Method = Method.ToString().Substring(5)

        ' Set form/post content-type if necessary
        If (Method = HTTPMethod.HTTP_POST AndAlso PostData <> "" AndAlso ContentType = "") Then
            ContentType = "application/x-www-form-urlencoded"
        End If

        ' Set Content-Type
        If (ContentType <> "") Then
            Request.ContentType = ContentType
            Request.ContentLength = PostData.Length
        End If

        ' Send Request, If Request
        If (Method = HTTPMethod.HTTP_POST) Then
            Try
                SW = New StreamWriter(Request.GetRequestStream())
                SW.Write(PostData)
            Catch Ex As Exception
                Throw Ex
            Finally
                SW.Close()
            End Try
        End If

        ' Receive Response
        Try
            Response = Request.GetResponse()
            SR = New StreamReader(Response.GetResponseStream())
            ResponseData = SR.ReadToEnd()
        Catch Wex As System.Net.WebException
            SR = New StreamReader(Wex.Response.GetResponseStream())
            ResponseData = SR.ReadToEnd()
            Throw New Exception(ResponseData)
        Finally
            SR.Close()
        End Try

        Return ResponseData
    End Function
End Class