NETWORK COMMUNICATION in Visual Basic .NET

Creation QR-Code in Visual Basic .NET NETWORK COMMUNICATION

CHAPTER 5 NETWORK COMMUNICATION
Print QR-Code In VB.NET
Using Barcode maker for VS .NET Control to generate, create Denso QR Bar Code image in .NET framework applications.
www.OnBarcode.com
Denso QR Bar Code Recognizer In VB.NET
Using Barcode recognizer for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
The HttpWebRequest Class
Code 128 Code Set A Drawer In Visual Basic .NET
Using Barcode generation for .NET Control to generate, create Code 128 Code Set A image in Visual Studio .NET applications.
www.OnBarcode.com
1D Generation In Visual Basic .NET
Using Barcode generator for Visual Studio .NET Control to generate, create 1D image in .NET applications.
www.OnBarcode.com
The HttpWebRequest is a specialization of the WebRequest class designed to communicate over the HTTP and HTTPS protocols. It also supports the GET and POST methods. Generally, if the host browser can do it, the HttpWebRequest can do it too, since this class leverages the host browser s networking. An instance of HttpWebRequest cannot be created directly. The WebRequest class contains a factory method named Create that returns an appropriate instance of a WebRequest inheritor, based on the protocol specified in the URI. Silverlight supports only the HTTP and HTTPS protocols, and both cause Create to return an instance of HttpWebRequest (actually, since HttpWebRequest is also abstract, a concrete implementation of HttpWebRequest is created; however, for all intents and purposes, it is an HttpWebRequest). For example, Uri uri = new Uri("http://www.technologyopinion.com"); HttpWebRequest myHttpWebRequest1= (HttpWebRequest)WebRequest.Create(uri); The HttpWebRequest class works in concert with HttpWebResponse to handle the data sent back from the server. The nature of communication using HttpWebRequest is also asynchronous; however, it utilizes the BeginXXX/EndXXX pattern that you may be familiar with from .NET. Tables 5-5 and 5-6 describe the key methods and properties of this class, respectively. Table 5-5. Key Methods of the System.Net.HttpWebRequest Class
Matrix Barcode Creator In Visual Basic .NET
Using Barcode creator for Visual Studio .NET Control to generate, create 2D Barcode image in VS .NET applications.
www.OnBarcode.com
Data Matrix 2d Barcode Printer In Visual Basic .NET
Using Barcode generation for .NET Control to generate, create DataMatrix image in VS .NET applications.
www.OnBarcode.com
Name
Encoding Barcode In VB.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Painting UPCE In Visual Basic .NET
Using Barcode creation for .NET framework Control to generate, create UPCE image in Visual Studio .NET applications.
www.OnBarcode.com
BeginGetRequestStream EndGetRequestStream BeginGetResponse EndGetResponse Abort
QR Code Decoder In .NET Framework
Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Scanning QR Code In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Description
EAN13 Generation In Java
Using Barcode creator for Java Control to generate, create GS1 - 13 image in Java applications.
www.OnBarcode.com
Printing Barcode In .NET Framework
Using Barcode creation for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
Begins an asynchronous request to obtain a Stream to write data. Returns a Stream. Use this in the asynchronous callback method passed to BeginGetRequestStream to get the Stream to write your request to. Begins an asynchronous request to communicate with a server. Returns a WebResponse; provides access to a Stream containing the data downloaded from the server. Cancels an executing asynchronous operation.
Data Matrix ECC200 Creation In .NET
Using Barcode printer for Reporting Service Control to generate, create DataMatrix image in Reporting Service applications.
www.OnBarcode.com
PDF 417 Creation In None
Using Barcode creation for Office Word Control to generate, create PDF 417 image in Word applications.
www.OnBarcode.com
Table 5-6. Key Properties of the System.Net.HttpWebRequest Class
Scan Data Matrix ECC200 In Visual Basic .NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Generating GTIN - 128 In .NET
Using Barcode creator for .NET Control to generate, create GS1-128 image in .NET applications.
www.OnBarcode.com
Name
Generating UPC - 13 In Java
Using Barcode printer for BIRT reports Control to generate, create EAN / UCC - 13 image in BIRT reports applications.
www.OnBarcode.com
Draw UPC - 13 In None
Using Barcode encoder for Online Control to generate, create EAN-13 image in Online applications.
www.OnBarcode.com
ContentType HaveResponse Headers Method RequestUri
EAN 128 Creation In None
Using Barcode encoder for Online Control to generate, create EAN / UCC - 14 image in Online applications.
www.OnBarcode.com
UPC-A Encoder In None
Using Barcode creator for Font Control to generate, create GTIN - 12 image in Font applications.
www.OnBarcode.com
Description
Corresponds to the Content-Type HTTP header. true if a response has been received; false otherwise. A collection containing the HTTP headers. Corresponds to the method used in the request. Currently, it can be only GET or POST. The URI of the request.
CHAPTER 5 NETWORK COMMUNICATION
The EndGetResponse of the HttpWebRequest class returns a WebResponse. Much like the WebRequest, the WebResponse is abstract and actually requires us to look one level deeper in the hierarchy, so let s take a look at the HttpWebResponse class. The HttpWebResponse class provides access to the data sent by the server to Silverlight. Its most important method is GetResponseStream, inherited from the WebResponse class. This method gives you a Stream containing the data sent by the server. When you are done with the response, make sure you call its Close method since the connection to the server remains open in the meantime. Tables 5-7 and 5-8 describe the key methods and properties of this class, respectively. Table 5-7. Methods of the System.Net.HttpWebResponse Class
Name
Close GetResponseStream
Description
Closes the stream and releases the connection to the server. Returns a Stream. Use this to access the data sent by the server to Silverlight.
Table 5-8. Properties of the System.Net.HttpWebResponse Class
Name
ContentLength ContentType ResponseUri
Description
Length of the data sent to Silverlight. MIME type of the content sent, if available. URI of the server that sent the response.
One way to use the HttpWebRequest class is to retrieve data from a server. In this case, we can go straight to using the BeginGetResponse method, since all we care about is retrieving data from a server, not sending data. The following code demonstrates that, based on the user-entered address to connect to the server to download data, such as downloading an HTML file from our site of origin. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create( new Uri(addressTB.Text)); request.BeginGetResponse(new AsyncCallback(responseHandler), request); The implementation of the response handler is where we read the response from the server. void responseHandler(IAsyncResult asyncResult) { try { HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState; HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult); StreamReader reader = new StreamReader(response.GetResponseStream()); string line; outputTB.Text = "";
CHAPTER 5 NETWORK COMMUNICATION
while ((line = reader.ReadLine()) != null) { outputTB.Text += line; } } catch (Exception ex) { outputTB.Text = ex.Message; } } In the response handler, we grab the request object via the AsyncState parameter, and then get the Stream from GetResponseStream. This is the equivalent of the HTTP GET method. Sending data to a server is similar to initiating an asynchronous operation for retrieving the response. BeginGetRequestStream starts the operation, and then EndGetRequestStream gives us the Stream in the asynchronous callback method passed to BeginGetRequestStream. This is equivalent to the HTTP POST method.
Copyright © OnBarcode.com . All rights reserved.