ASP 101 - Active Server Pages 101 - Web03
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash





ASP.NET version of "HTTP Request"


When compared to an http script written in standard ASP, this script really illustrates how important a role objects play in the new .NET framework. It seems like every object returns an object, and while it may take some getting used to, it really does seem to make more sense.

That being said... it'll take a while to get used to! ;)

Here's a zip file of the code (1.1 KB).

Play with the running version.

View the live source code.


I recently (Jan. 16, 2002) got an interesting note about this sample from one of our readers and I thought that I should share it with everyone:

Hi John,

I'm writting in regards to your article at http://www.asp101.com/samples/http.asp ~ June 2001?

I forget how I fell upon it just now and I'm sure you've long since forgotten the code, but it did remind me of how, looking forward as I am to .net and, likely, C#, I'll miss VB's with statement...

One glance at (hopefully hotmail will preserve the formatting)...

Function GetWebPageAsStringShort(strURI As String) As String
	Dim objURI As URI = New URI(strURI)
	Dim objWebRequest As WebRequest = WebRequest.Create(objURI)
	Dim objWebResponse As WebResponse = objWebRequest.GetResponse()
	Dim objStream As Stream = objWebResponse.GetResponseStream()
	Dim objStreamReader As StreamReader = New StreamReader(objStream)
	Dim strHTML As String = objStreamReader.ReadToEnd
	GetWebPageAsStringShort = strHTML
End Function

...and I couldn't resist whipping up...

Function GetWebPageAsStringShort2(strURI As String) As String
	With WebRequest.Create(New URI(strURI)).GetResponse()
	    With New StreamReader(.GetResponseStream())
			GetWebPageAsStringShort2 = .ReadToEnd()
	    End With
	End With
End Function

Admittedly, I'm partial to my version, but I particularly like how the use of the with statement results in a fn that introduces a reader to only two new tokens, the names of the fn and its arg. To be certain, I'm not sure of the details of .net memory management, but, I believe this version is at least as prudent an implementation since the compiler can early-bind with references, perhaps more so as I also create and leave what I can on the stack.

The manner in which indentation in conjunction with progressively shorter lines of code focuses the eye gradually towards the end result is an especially neat side effect. (Yes, I studied visual arts prior to cs ;~)

Of course, it is but one of so many bits of code we'll write in a lifetime. And, I've spent more time writting this email than the fn!

What do you think?

Take Care,
JT from Boston, MA.


Back to the "HTTP Request" sample


Samples

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers