A simple HTTP request for the contents of another file on the server in .NET framework

Creation PDF-417 2d barcode in .NET framework A simple HTTP request for the contents of another file on the server

Listing 5.12 A simple HTTP request for the contents of another file on the server
PDF-417 2d Barcode Encoder In .NET Framework
Using Barcode generator for ASP.NET Control to generate, create PDF 417 image in ASP.NET applications.
www.OnBarcode.com
Draw PDF 417 In .NET
Using Barcode generation for ASP.NET Control to generate, create PDF417 image in ASP.NET applications.
www.OnBarcode.com
var request = new Sys.Net.WebRequest(); request.set_url("message.txt"); request.add_completed(onRequestComplete); request.invoke();
Making Matrix 2D Barcode In VS .NET
Using Barcode encoder for ASP.NET Control to generate, create 2D Barcode image in ASP.NET applications.
www.OnBarcode.com
GS1 - 13 Generation In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create EAN 13 image in ASP.NET applications.
www.OnBarcode.com
The first step in putting together a request is to create an instance of the Sys.Net.WebRequest object. Then, you set the url property of the request to the file on the server. Next, you add an event handler for when the request completes by calling the add_completed function and passing in the name of the routine. The final statement in listing 5.12 is a call to the invoke method, which is responsible for issuing the asynchronous request.
Drawing QR Code ISO/IEC18004 In Visual Studio .NET
Using Barcode creator for ASP.NET Control to generate, create QR Code ISO/IEC18004 image in ASP.NET applications.
www.OnBarcode.com
Creating Barcode In .NET
Using Barcode creation for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
NOTE
UCC.EAN - 128 Creator In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create UCC - 12 image in ASP.NET applications.
www.OnBarcode.com
Make 4-State Customer Barcode In VS .NET
Using Barcode maker for ASP.NET Control to generate, create OneCode image in ASP.NET applications.
www.OnBarcode.com
The add_completed function should be called before the invoke method on the WebRequest instance. If the browser has the message.txt file already in its cache, you don t need to issue an HTTP request to the server. In this case, the request completes synchronously, and the onRequestComplete handler is called before the invoke method returns.
Recognizing PDF-417 2d Barcode In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
PDF417 Printer In Visual Studio .NET
Using Barcode generator for Reporting Service Control to generate, create PDF-417 2d barcode image in Reporting Service applications.
www.OnBarcode.com
Let s look now at the callback routine, onRequestComplete. Here, you receive the contents of the file requested from the server:
PDF 417 Generator In None
Using Barcode creator for Office Word Control to generate, create PDF417 image in Microsoft Word applications.
www.OnBarcode.com
Code 128C Scanner In C#
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
function onRequestComplete(executor, eventArgs) { alert(executor.get_responseData()); }
Reading Data Matrix 2d Barcode In VB.NET
Using Barcode scanner for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Draw QR Code 2d Barcode In C#
Using Barcode generator for VS .NET Control to generate, create Denso QR Bar Code image in Visual Studio .NET applications.
www.OnBarcode.com
Making asynchronous network calls
Recognize QR-Code In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Code-39 Decoder In C#.NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
The onRequestComplete function is called with two parameters. The first, executor, is of type Sys.Net.WebRequestExecutor, and contains all the information about the status of the asynchronous request. The second, eventArgs, is always set to Sys.EventArgs.Empty an object that represents the absence of event arguments.
UPC-A Decoder In .NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Barcode Creation In .NET Framework
Using Barcode printer for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
NOTE
EAN 13 Decoder In .NET Framework
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Generate Barcode In Java
Using Barcode creator for BIRT Control to generate, create Barcode image in BIRT reports applications.
www.OnBarcode.com
Sys.EventArgs.Empty plays the same role as the System.EventArgs.Empty object that is passed to event handlers in the .NET framework to indicate the absence of event arguments.
To retrieve the contents of the file, you can call the get_responseData method of the executor object. If the response completes successfully, the content of the message.txt file is returned. In section 5.2.4, we ll examine what happens when a request fails and how to handle it cleanly. This executor object is important. Let s discuss its function in the process.
The executor
The executor object that you accessed in the earlier example is an instance of the Sys.Net.XMLHttpExecutor class. In turn, this class inherits from Sys.Net.WebRequestExecutor, which acts as an abstract class. By overriding the implementation of the executeRequest method, you can specify how an HTTP request is sent from script. For example, the default executor, Sys.Net.XMLHttpExecutor, sends a request using the XMLHttpRequest object. Other types of executors can be created to implement different techniques for sending asynchronous requests to the server.
NOTE
At the moment, the XMLHttpExecutor is the only executor provided by the Microsoft Ajax Library. Previous CTPs included other executors, such as the IFrameExecutor. However these executors were omitted from the final release for quality and security reasons.
The executor object provides all the information you need to know about the response sent by the web server. If you re expecting data in an XML format, use the get_xml method to retrieve the response in the form of an XML DOM object. Data returned in a JSON format can be retrieved with the get_object method. The executor also offers methods that you can use to examine the status code and text of each response:
var statusCode = executor.get_statusCode(); var statusText = executor.get_statusText();
Facilitating all this interaction is a single object on the client called the WebRequestManager. To help you understand how all the pieces fit together, we ll continue our exploration by briefly looking at how this object is used and where it fits.
The asynchronous communication layer
WebRequestManager
The WebRequestManager is an instance of the Sys.Net._WebRequestManager class. When the Microsoft Ajax runtime is loaded, the instance is created and stored in a global JavaScript variable called Sys.Net.WebRequestManager. When the invoke method is called on a WebRequest object, the request is passed to the WebRequestManager. Here, checks are made to determine the associated implementation of the executor object for the request. If an assigned executor isn t found, then the default XMLHttpExecutor is used. At this point, the WebRequestManager calls the executeRequest method on the executor object to launch the request. This leads us to errors and how you should handle them correctly and efficiently. In the next section, you ll put together a useful error-handling mechanism that can be added to your toolbox for future Ajax development.
Copyright © OnBarcode.com . All rights reserved.