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 Gregory [Jan 05, 2005 @ 4:49 AM]
You need to add this to the top of the class :-)
' EasyHTTP.vb class file
Imports System.Net
Imports System.IO
Public Class EasyHTTP
'blahblah
End Class
post reply to this comment Comment by Carl [Jan 20, 2006 @ 1:35 AM]
Sorry for the nOOb-ness but I got a quick question.
I have
EasyHttp.Send("http://" + Xip.Text + "/xbmcCmds/xbmcHttp?command=execbuiltin&parameter=" + xcom)

but I get this error message
An unhandled exception of type 'System.NullReferenceException' occurred in XBMCControl.exe

I have tried 2 things.
1) Switching it to a POST.. but it just hangs the app
2)Messing around with the URL.. it appears if I alter the "xbmcHttp" part.. making it anything but "http" it  sends fine, to the wrong URL ;-)  I'm assuing its doing something wacky when it see's the letters HTTP in there again??

Any help would be great, thanks
~Carl
post reply to this comment Comment by Tony [Nov 27, 2006 @ 9:56 PM]
Cool but, can you give more examples of the usage. For example if I want to send a file to another webserver, what would the call be?
post reply to this comment Comment by Andy [Jan 31, 2007 @ 3:50 AM]
EasyHTTP is great, thank-you. I'm about to start using it over SSL with certificates and would appreciate any pointers on how I would need to modify your class?
If not, I'll just figure it out for myself and try to post back the finished code.

Cheers
post reply to this comment Comment by Tomer [Feb 15, 2007 @ 12:58 AM]
EasyHTTP look very easy to use but i just past the code and try to send post i got this message:
The underlying connection was closed: Unable to connect to the remote server.

can any one help with an idea to resolve this problem ?
post reply to this comment Comment by Ted [Aug 09, 2007 @ 12:17 AM]
I tried passing an XML string to a webservice and I'm getting "Request Error. No XML." back.  See code below.  Any help will be appreciated. Public Shared Function Send(ByVal URL As String, _         ByVal PostData As String, _         Optional ByVal Method As HTTPMethod = HTTPMethod.HTTP_POST, _         Optional ByVal ContentType As String = "text/xml")         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
Leave Your Comment
Name:
Email:  (gravatar enabled)
URL:
Comment:
or Cancel