WebRequest and WebResponse

WebRequest is an abstract base class provided by .NET Framework for accessing data from the internet. Using this class, we send a request to a particular URL, such as www.google.com

On the other hand, WebResponse is an abstract class that provides a response from the URL called by the WebRequest class. 

The WebRequest object is created by calling the static Create method. In the method, we pass the address URL that we want to call in the request. The request inspects the address we are passing to it and selects a protocol implementation, for example, HTTP or FTP. Based upon the web address passed, an appropriate instance of the derived class, such as HttpWebRequest for HTTP or FtpWebRequest for FTP, is returned when the WebRequest object is created. The WebRequest class also allows us to specify some other properties, such as the authentication and content type. Let's go through the following code implementation, which will help us learn more about this class:

WebRequest request = null;
HttpWebResponse response = null;
Stream dataStream = null;
StreamReader reader = null;
try
{
request = WebRequest.Create("http://www.google.com/search?q=c#");
request.Method = "GET"; response = (HttpWebResponse)request.GetResponse();
dataStream = response.GetResponseStream();
reader = new StreamReader(dataStream);
Console.WriteLine(reader.ReadToEnd());
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
reader.Close();
dataStream.Close();
response.Close();
}

In the preceding code implementation, we are creating a WebRequest object for http://google.com.  We are using a GET method in the HTTP request and passing parameters embedded in the URL itself. As the protocol is HTTP, we are converting the WebResponse object to httpWebResponse.

Once we have captured the response, we are retrieving the stream of bytes into a  Stream object and are then using a StreamReader object to retrieve the response from google.com.

A very important thing to note here is that in the finally block, we are closing all the response, stream, and reader objects that have been created in the try...catch block. This is essential: as we are dealing with unmanaged resources, it's important to reclaim the memory for better performance. 

For further reading, please refer to the following blog from Microsoft, which discusses the different parameters that we can set in the WebRequest object when we are making a call: https://docs.microsoft.com/en-us/dotnet/api/system.net.webrequest?view=netframework-4.7.2.

In the preceding code, we were making synchronous calls to the external web service and waiting for a response. However, in a real-world scenario, this may not be the ideal implementation. 

The external server to which we are making a call may take some time to send us the response. Therefore, if our application continues to wait for a response during this time, the responsiveness of the application will be challenged. To avoid such scenarios, we can make I/O calls asynchronous. In the next section, we will learn why we need to look at making asynchronous I/O calls and how they're implemented in code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.218.226