Learn ASP.NET MVC, ASP.NET Core, and Design Patterns through our online training programs. Courses conducted by Bipin Joshi on weekends. Read more details here.

Using WebRequest and WebResponse classes

Introduction

.NET framework provides extensive set of classes for network programming. The System.Net namespace contains many such classes. In order to develop applications that involve web and HTTP .NET provides two important classes - WebRequest and WebResponse. There is another class called WebClient that actually simplifies tasks like downloading and uploading files, posting forms etc. In this article we will see how to use WebRequest and WebResponse classes to perform common tasks like requesting a URL and downloading a file.

How they work?

The WebRequest and WebResponse classes are typically used together. The WebRequest class makes a request to a web resporce like HTML/ASPX file. The WebResponse class then reads the returned response that might be result of processing or some other data. While developing applications using these classes following sequence is used most commonly :
  • Create a WebResquest object
  • Make a request and get response from back into WebResponse object
  • Read response stream for any data
  • Use the data either for processing or storing in file
  • Close the streams

Example 1 - Displaying HTML source of a HTML page

Now, let us see an example of how to use these objects in your applications. We will make a request to a HTML page and display response returned (which is nothing but HTML tags and content) in a textbox.
Dim request As WebRequest
Dim response As WebResponse
Dim reader As Stream
Dim data(1023) As Byte
Dim count As Integer
Dim strTemp As String
Dim strTotal As New StringBuilder()
Dim myencoding As New ASCIIEncoding()

request = WebRequest.Create("http://somedomain/index.htm")
response = request.GetResponse()
reader = response.GetResponseStream()

While True
	count = reader.Read(data, 0, 1024)
	If count <= 0 Then
		Exit While
	End If
	strTemp = myencoding.GetString(data)
	strTotal.Append(strTemp)
End While

reader.Close()
Let us examine the code in detail :
  • We have declared required variables of type WebRequest and WebResponse
  • WebRequest class has a static method called Create that accepts web URL as parameter
  • The response is returned back when you called GetResponse() method of WebRequest object
  • In order to access data returned we need to use the response stram that is obtained via GetResponseStream() method of WebResponse class
  • The data is read into a byte array. Note that since we are using VB.NET the array declaration specifies 1023 i.e.0-1023. If you are using C# make sure to change it to 1024
  • The data returned by the stream is in the form of bytes. This data is converted into string using ASCIIEncoding class found in System.Text namespace
  • We are using StringBuilder for increasing performance while concatening string values
  • Finally we close the stream and display the data in a text box

Example 2 - Downloading a file from web

This example is similar to above one but stores response returned in a disk file rather than displaying it in textbox.
Dim request As WebRequest
Dim response As WebResponse
Dim reader As Stream
Dim writer As Stream
Dim data(1023) As Byte
Dim count As Integer

request = WebRequest.Create("http://somedomain/mydata.zip")
response = request.GetResponse()
reader = response.GetResponseStream()

writer = File.Create("mylocaldata.zip")
While True
	count = reader.Read(data, 0, 1024)
	If count <= 0 Then
		Exit While
	End If
	writer.Write(data, 0, count)
End While

reader.Close()
writer.Close()
Here, we are requesting a zip file called mydata.zip. We then write all the response data into a local file called mylocaldata.zip. As usual we close both the streams after the work is done.



Bipin Joshi is a software consultant, trainer, author and a yogi having 21+ years of experience in software development. He conducts online courses in ASP.NET MVC / Core, jQuery, AngularJS, and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced Yoga way of life he also teaches Ajapa Meditation to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 25 Jan 2004



Tags : ASP.NET .NET Framework Components