qr code generator api c# Handling Network Errors in C#

Printer QR Code in C# Handling Network Errors

Handling Network Errors
Quick Response Code Encoder In Visual C#
Using Barcode generation for Visual Studio .NET Control to generate, create Quick Response Code image in .NET applications.
QR Code JIS X 0510 Decoder In Visual C#.NET
Using Barcode scanner for .NET framework Control to read, scan read, scan image in VS .NET applications.
Although the program in the preceding section is correct, it is not resilient Even the simplest network error will cause it to end abruptly Although this isn t a problem for the example programs shown in this chapter, it is something that must be avoided in real-world applications To fully handle all network exceptions that the program might generate, you must monitor calls to Create( ), GetResponse( ), and GetResponseStream( ) It is important to understand that the exceptions that can be generated depend upon the protocol being used The following discussion describes the errors possible when using HTTP
Drawing Barcode In C#
Using Barcode drawer for .NET Control to generate, create barcode image in .NET applications.
Reading Barcode In Visual C#.NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
Exceptions Generated by Create( )
Denso QR Bar Code Maker In .NET
Using Barcode creator for ASP.NET Control to generate, create QR Code 2d barcode image in ASP.NET applications.
Denso QR Bar Code Encoder In .NET Framework
Using Barcode creation for Visual Studio .NET Control to generate, create QR-Code image in .NET framework applications.
The Create( ) method defined by WebRequest can generate four exceptions If the protocol specified by the URI prefix is not supported, then NotSupportedException is thrown If the URI format is invalid, UriFormatException is thrown If the user does not have the proper authorization, a SystemSecuritySecurityException will be thrown Create( ) can also throw an ArgumentNullException if it is called with a null reference, but this is not an error generated by networking
Paint QR-Code In VB.NET
Using Barcode creation for VS .NET Control to generate, create QR Code image in VS .NET applications.
Code 128C Maker In Visual C#
Using Barcode creation for Visual Studio .NET Control to generate, create Code128 image in .NET framework applications.
Exceptions Generated by GetReponse( )
UPC A Drawer In Visual C#.NET
Using Barcode encoder for .NET framework Control to generate, create UPC-A Supplement 5 image in Visual Studio .NET applications.
Creating GTIN - 128 In Visual C#.NET
Using Barcode maker for .NET Control to generate, create EAN 128 image in .NET applications.
A number of errors can occur when obtaining an HTTP response by calling GetResponse( ) These are represented by the following exceptions: InvalidOperationException, ProtocolViolationException, NotSupportedException, and WebException Of these, the one of most interest is WebException
DataMatrix Maker In C#
Using Barcode creation for VS .NET Control to generate, create Data Matrix 2d barcode image in Visual Studio .NET applications.
Code11 Generation In Visual C#.NET
Using Barcode creator for .NET Control to generate, create Code 11 image in Visual Studio .NET applications.
25:
Create Code 3 Of 9 In None
Using Barcode printer for Font Control to generate, create Code-39 image in Font applications.
Code-39 Decoder In .NET
Using Barcode reader for .NET Control to read, scan read, scan image in .NET framework applications.
Networking Through the Internet Using SystemNet
Encoding ECC200 In .NET
Using Barcode creation for Visual Studio .NET Control to generate, create Data Matrix image in Visual Studio .NET applications.
Make UCC - 12 In None
Using Barcode encoder for Office Excel Control to generate, create EAN 128 image in Excel applications.
WebException has two properties that relate to network errors: Response and Status You can obtain a reference to the WebResponse object inside an exception handler through the Response property For the HTTP protocol, this object describes the error It is defined like this: public WebResponse Response { get; } When an error occurs, you can use the Status property of WebException to find out what went wrong It is defined like this: public WebExceptionStatus Status {get; } WebExceptionStatus is an enumeration that contains the following values:
UPC A Creation In Java
Using Barcode generator for Java Control to generate, create GS1 - 12 image in Java applications.
Bar Code Scanner In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
CacheEntryNotFound KeepAliveFailure Pending ProxyNameResolutionFailure RequestProhibitedByCachePolicy SendFailure Timeout ConnectFailure MessageLengthLimitExceeded PipelineFailure ReceiveFailure RequestProhibitedByProxy ServerProtocolViolation TrustFailure ConnectionClosed NameResolutionFailure ProtocolError RequestCanceled SecureChannelFailure Success UnknownError
Generate EAN13 In .NET Framework
Using Barcode printer for ASP.NET Control to generate, create EAN 13 image in ASP.NET applications.
Paint Bar Code In .NET
Using Barcode generator for ASP.NET Control to generate, create barcode image in ASP.NET applications.
PART II
Once the cause of the error has been determined, your program can take appropriate action
Exceptions Generated by GetResponseStream( )
For the HTTP protocol, the GetResponseStream( ) method of WebResponse can throw a ProtocolViolationException, which, in general, means that some error occurred relative to the specified protocol As it relates to GetResponseStream( ), it means that no valid response stream is available An ObjectDisposedException will be thrown if the response has already been disposed Of course, an IOException could occur while reading the stream, depending on how input is accomplished
Using Exception Handling
The following program adds handlers for all possible network exceptions to the example shown earlier:
// Handle network exceptions using System; using SystemNet; using SystemIO; class NetExcDemo { static void Main() { int ch; try { // First, create a WebRequest to a URI HttpWebRequest req = (HttpWebRequest)
Part II:
Exploring the C# Library
WebRequestCreate("http://wwwMcGraw-Hillcom"); // Next, send that request and return the response HttpWebResponse resp = (HttpWebResponse) reqGetResponse(); // From the response, obtain an input stream Stream istrm = respGetResponseStream(); /* Now, read and display the html present at the specified URI So you can see what is being displayed, the data is shown 400 characters at a time After each 400 characters are displayed, you must press ENTER to get the next 400 */ for(int i=1; ; i++) { ch = istrmReadByte(); if(ch == -1) break; ConsoleWrite((char) ch); if((i%400)==0) { ConsoleWrite("\nPress Enter"); ConsoleReadLine(); } } // Close the response This also closes istrm respClose(); } catch(WebException exc) { ConsoleWriteLine("Network Error: " + excMessage + "\nStatus code: " + excStatus); } catch(ProtocolViolationException exc) { ConsoleWriteLine("Protocol Error: " + excMessage); } catch(UriFormatException exc) { ConsoleWriteLine("URI Format Error: " + excMessage); } catch(NotSupportedException exc) { ConsoleWriteLine("Unknown Protocol: " + excMessage); } catch(IOException exc) { ConsoleWriteLine("I/O Error: " + excMessage); } catch(SystemSecuritySecurityException exc) { ConsoleWriteLine("Security Exception: " + excMessage); } catch(InvalidOperationException exc) { ConsoleWriteLine("Invalid Operation: " + excMessage); } } }
Now the exceptions that the networking methods might generate have been caught For example, if you change the call to Create( ) as shown here,
WebRequestCreate("http://wwwMcGraw-Hillcom/moonrocket");
25:
Copyright © OnBarcode.com . All rights reserved.