Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' EasyHttp.vb
- '
- ' Author: Odysseus (Punkbastard)
- ' Date: May 18th, 2015
- ' Last edit: June 30th, 2015
- Imports System.Text
- Imports System.IO
- Imports System.Net
- Public Class EasyHttp
- Dim _source As String
- Dim cookieContainer As New CookieContainer
- Dim UserAgent As String = "EasyHttp/2.0 (VB.NET, .NET Framework 2.0)"
- Property UserAgent() As String
- Get
- Return UserAgent
- End Get
- Set(ByVal input As String)
- UserAgent = input
- End Set
- End Property
- Public Function HttpGet(ByVal uri As String) As String
- Try
- ' Create GET Request to given Uri
- Dim getRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(uri), HttpWebRequest)
- getRequest.Method = "GET"
- getRequest.CookieContainer = Me.cookieContainer
- getRequest.UserAgent = UserAgent
- ' Read Response
- Using gReader As New StreamReader(getRequest.GetResponse().GetResponseStream())
- Me._source = gReader.ReadToEnd()
- End Using
- ' Return return the response
- Return Me._source
- Catch ex As Exception
- ' Something went terribly wrong, return the error
- Return ex.Message
- End Try
- End Function
- Public Function HttpPost(ByVal uri As String, ByVal postData As String) As String
- Try
- ' Create a POST request to given Uri with given postData
- Dim postRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(uri), HttpWebRequest)
- postRequest.Method = "POST"
- postRequest.CookieContainer = Me.cookieContainer
- postRequest.UserAgent = UserAgent
- postRequest.ContentLength = postData.Length
- postRequest.ContentType = "application/x-www-form-urlencoded"
- ' Handle Post Data
- Dim postEncoding As New UTF8Encoding()
- Dim postDataBytes As Byte() = postEncoding.GetBytes(postData)
- postRequest.ContentLength = postDataBytes.Length
- ' Write postData to Stream
- Using pStream As Stream = postRequest.GetRequestStream()
- pStream.Write(postDataBytes, 0, postDataBytes.Length)
- End Using
- ' Read Response
- Using pReader As New StreamReader(DirectCast(postRequest.GetResponse(), HttpWebResponse).GetResponseStream())
- Me._source = pReader.ReadToEnd()
- End Using
- ' Return true, means success
- Return Me._source
- Catch ex As Exception
- ' Something terribly went wrong, return the error message
- Return ex.Message
- End Try
- End Function
- End Class
Add Comment
Please, Sign In to add comment