C# 0 The Complete Reference


Exceptions Generated by GetResponseStream( )



Download 4,07 Mb.
Pdf ko'rish
bet1031/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   1027   1028   1029   1030   1031   1032   1033   1034   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

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 System.Net;

using System.IO;

class NetExcDemo {

  static void Main() {

    int ch;

    try {

      // First, create a WebRequest to a URI.

      HttpWebRequest req = (HttpWebRequest)

www.freepdf-books.com




832

 

P a r t   I I :  



E x p l o r i n g   t h e   C #   L i b r a r y

             WebRequest.Create("http://www.McGraw-Hill.com");

      // Next, send that request and return the response.

      HttpWebResponse resp = (HttpWebResponse)

             req.GetResponse();

      // From the response, obtain an input stream.

      Stream istrm = resp.GetResponseStream();

      /* 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 =  istrm.ReadByte();

        if(ch == -1) break;

        Console.Write((char) ch);

        if((i%400)==0) {

          Console.Write("\nPress Enter.");

          Console.ReadLine();

        }

      }


      // Close the response. This also closes istrm.

      resp.Close();

    } catch(WebException exc) {

      Console.WriteLine("Network Error: " + exc.Message +

                        "\nStatus code: " + exc.Status);

    } catch(ProtocolViolationException exc) {

      Console.WriteLine("Protocol Error: " + exc.Message);

    } catch(UriFormatException exc) {

      Console.WriteLine("URI Format Error: " + exc.Message);

    } catch(NotSupportedException exc) {

      Console.WriteLine("Unknown Protocol: " + exc.Message);

    } catch(IOException exc) {

      Console.WriteLine("I/O Error: " + exc.Message);

    } catch(System.Security.SecurityException exc) {

      Console.WriteLine("Security Exception: " + exc.Message);

    } catch(InvalidOperationException exc) {

      Console.WriteLine("Invalid Operation: " + exc.Message);

    }


  }

}

Now the exceptions that the networking methods might generate have been caught. For 



example, if you change the call to 

Create( )

 as shown here,

WebRequest.Create("http://www.McGraw-Hill.com/moonrocket");

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   1027   1028   1029   1030   1031   1032   1033   1034   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish