C# 0 The Complete Reference


PART II C h a p t e r   2 5



Download 4,07 Mb.
Pdf ko'rish
bet1023/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   1019   1020   1021   1022   1023   1024   1025   1026   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART II

C h a p t e r   2 5 :  

N e t w o r k i n g   T h r o u g h   t h e   I n t e r n e t   U s i n g   S y s t e m . N e t    

827


properties are used later in this chapter. However, for simple Internet operations, you will 

not often need to use these extra capabilities.



A Simple First Example

Internet access centers around 



WebRequest

 and 


WebResponse

. Before we examine the 

process in detail, it will be useful to see an example that illustrates the request/response 

approach to Internet access. After you see these classes in action, it is easier to understand 

why they are organized as they are.

The following program performs a simple, yet very common, Internet operation. It 

obtains the hypertext contained at a specific website. In this case, the content of McGraw-

Hill.com is obtained, but you can substitute any other website. The program displays the 

Method

Description



public vir tual void Close( )

Closes the response. It also closes 

the response stream returned by 

GetResponseStream( )

.

public vir tual Stream GetResponseStream( )



Returns an input stream connected to the 

requested URI. Using this stream, data can 

be read from the URI.

T

ABLE



 25-3 

Commonly Used Methods Defi ned by 

WebResponse

Property


Description

public vir tual long ContentLength { get; set; }

Obtains or sets the length of the content 

being received. This will be –1 if the content 

length is not available.

public vir tual string ContentType { get; set; }

Obtains or sets a description of the content.

public vir tual WebHeaderCollection

    Headers { get; }

Obtains a collection of the headers 

associated with the URI.

public vir tual bool IsFromCache { get; }

If the response came from the cache, this 

proper ty is true. It is false if the response 

was delivered over the network. 

public vir tual bool

    IsMutuallyAuthenticated { get; }

If the client and server are both authenticated, 

then this property is true. It is false otherwise. 

public vir tual Uri ResponseUri { get; }

Obtains the URI that generated the response. 

This may differ from the one requested if the 

response was redirected to another URI.

T

ABLE



 25-4 

The Proper ties Defi ned by 

WebResponse

www.freepdf-books.com




828

 

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

hypertext on the screen in chunks of 400 characters, so you can see what is being received 

before it scrolls off the screen.

// Access a website.

using System;

using System.Net;

using System.IO;

class NetDemo {

  static void Main() {

    int ch;

    // First, create a WebRequest to a URI.

    HttpWebRequest req = (HttpWebRequest)

           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();

  }


}

The first part of the output is shown here. (Of course, over time this content will differ 

from that shown here.)





Home - The McGraw-Hill Companies



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   1019   1020   1021   1022   1023   1024   1025   1026   ...   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