Punkbastard

EasyHttp - VB.NET

May 18th, 2015
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.71 KB | None | 0 0
  1. ' EasyHttp.vb
  2. '
  3. ' Author: Odysseus (Punkbastard)
  4. ' Date: May 18th, 2015
  5. ' Last edit: June 30th, 2015
  6.  
  7. Imports System.Text
  8. Imports System.IO
  9. Imports System.Net
  10.  
  11. Public Class EasyHttp
  12.  
  13.     Dim _source As String
  14.     Dim cookieContainer As New CookieContainer
  15.    
  16.     Dim UserAgent As String = "EasyHttp/2.0 (VB.NET, .NET Framework 2.0)"
  17.    
  18.     Property UserAgent() As String
  19.         Get
  20.             Return UserAgent
  21.         End Get
  22.         Set(ByVal input As String)
  23.             UserAgent = input
  24.         End Set
  25.     End Property
  26.  
  27.     Public Function HttpGet(ByVal uri As String) As String
  28.         Try
  29.             ' Create GET Request to given Uri
  30.             Dim getRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(uri), HttpWebRequest)
  31.             getRequest.Method = "GET"
  32.             getRequest.CookieContainer = Me.cookieContainer
  33.             getRequest.UserAgent = UserAgent
  34.  
  35.             ' Read Response
  36.             Using gReader As New StreamReader(getRequest.GetResponse().GetResponseStream())
  37.                 Me._source = gReader.ReadToEnd()
  38.             End Using
  39.  
  40.             ' Return return the response
  41.             Return Me._source
  42.         Catch ex As Exception
  43.             ' Something went terribly wrong, return the error
  44.             Return ex.Message
  45.         End Try
  46.     End Function
  47.  
  48.     Public Function HttpPost(ByVal uri As String, ByVal postData As String) As String
  49.         Try
  50.            ' Create a POST request to given Uri with given postData
  51.             Dim postRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(uri), HttpWebRequest)
  52.             postRequest.Method = "POST"
  53.             postRequest.CookieContainer = Me.cookieContainer
  54.             postRequest.UserAgent = UserAgent
  55.             postRequest.ContentLength = postData.Length
  56.             postRequest.ContentType = "application/x-www-form-urlencoded"
  57.  
  58.             ' Handle Post Data
  59.             Dim postEncoding As New UTF8Encoding()
  60.             Dim postDataBytes As Byte() = postEncoding.GetBytes(postData)
  61.             postRequest.ContentLength = postDataBytes.Length
  62.  
  63.             ' Write postData to Stream
  64.             Using pStream As Stream = postRequest.GetRequestStream()
  65.                 pStream.Write(postDataBytes, 0, postDataBytes.Length)
  66.             End Using
  67.  
  68.             ' Read Response
  69.             Using pReader As New StreamReader(DirectCast(postRequest.GetResponse(), HttpWebResponse).GetResponseStream())
  70.                 Me._source = pReader.ReadToEnd()
  71.             End Using
  72.  
  73.             ' Return true, means success
  74.             Return Me._source
  75.         Catch ex As Exception
  76.             ' Something terribly went wrong, return the error message
  77.             Return ex.Message
  78.         End Try
  79.     End Function
  80.  
  81. End Class
Add Comment
Please, Sign In to add comment