Home | Software | WebLog | Contact | Wish List

EasyHttp.NET (VB.NET)

.Net has made a lot of things easier for us developers.  One of these things is the way we do HTTP Post and HTTP Get.  In classic ASP 3 you were forced to use 3rd party controls.  Below I have provided an example class (in Visual Basic.Net) on how send either HTTP Posts or HTTP Gets.

If you found this or any other code on this site usefull and want to show your appreciation, now you can. Thanks!

Example Code

' Examples of use Response.Write(EasyHttp.Send("http://yahoo.com")) Response.Write(EasyHttp.Send("http://search.yahoo.com/bin/search", "p=test"))

EasyHttp.vb class file

' 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 = "") 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



post reply to this comment Comment by Pir Qaiser [Feb 20, 2007 @ 10:11 PM]
Thats a great contribution. Worked perfectly. Not to forget

imports system.IO

post reply to this comment Comment by Robin Bonin [Jan 09, 2009 @ 6:28 PM]
Thanks for the class. I've been using it (or a slight variation) for several years now on various projects.
post reply to this comment Comment by Robin Bonin [Jan 09, 2009 @ 6:29 PM]
One other thing, if you are using this from a windows application you will also need to import system.net
post reply to this comment Comment by Brian Wetherell [Dec 13, 2009 @ 11:10 PM]
Just starting to learn about vb.net, and I was wondering what would it look like to post text to a URL using this class?
Leave Your Comment
Name:
Email:  (gravatar enabled)
URL:
Comment:
or Cancel