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
843
class WebClientDemo {
static void Main() {
WebClient user = new WebClient();
string uri = "http://www.McGraw-Hill.com";
string fname = "data.txt";
try {
Console.WriteLine("Downloading data from " +
uri + " to " + fname);
user.DownloadFile(uri, fname);
} catch (WebException exc) {
Console.WriteLine(exc);
}
Console.WriteLine("Download complete.");
}
}
Property
Description
public string BaseAddress { get; set; }
Obtains or sets the base address of the desired
URI. If this proper ty is set, then addresses
specified by the
WebClient
methods will be
relative to the base address.
public RequestCachePolicy
CachePolicy { get; set; }
Obtains or sets the policy that determines when
the cache is used.
public ICredentials Credentials { get; set; }
Obtains or sets authentication information. This
proper ty is null by default.
public Encoding Encoding { get; set; }
Obtains or sets the character encoding used while
transferring strings.
public WebHeaderCollection
Headers{ get; set; }
Obtains or sets the collection of the request
headers.
public bool IsBusy( get; }
If the request is still transferring information, this
proper ty is true. It is false other wise.
public IWebProxy Proxy { get; set; }
Obtains or sets the proxy.
public NameValueCollection
Quer yString { get; set; }
Obtains or sets a quer y string consisting of name/
value pairs that can be attached to a request. The
quer y string is separated from the URI by a
?
. If
more than one name/value pair exists, then an @
separates each pair.
public WebHeaderCollection
ResponseHeaders{ get; }
Obtains a collection of the response headers.
public bool UseDefaultCredentials { get; set; }
Obtains or sets a value that determines if default
credentials are used for authentication. If true,
the default credentials (i.e., those of the user)
are used. They are not used if false.
T
ABLE
25-6
The Proper ties Defi ned by
WebClient
www.freepdf-books.com
844
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
Method
Description
public byte[ ] DownloadData(string
uri
)
Downloads the information at the URI specified by
uri
and
returns the result in an array of bytes.
public byte[ ] DownloadData(Uri
uri
)
Downloads the information at the URI specified by
uri
and
returns the result in an array of bytes.
public void
DownloadFile(string
uri
, string
fname
)
Downloads the information at the URI specified by
uri
and
stores the result in the file specified by
fname.
public void
DownloadFile(Uri
uri
, string
fname
)
Downloads the information at the URI specified by
uri
and
stores the result in the file specified by
fname.
public string DownloadString(string
uri
)
Downloads the information at the URI specified by
uri
and
returns the result as a
string
.
public string DownloadString(Uri
uri
)
Downloads the information at the URI specified by
uri
and
returns the result as a
string
.
public Stream OpenRead(string
uri
)
Returns an input stream from which the information at
the URI specified by
uri
can be read. This stream must be
closed after reading is completed.
public Stream OpenRead(Uri
uri
)
Returns an input stream from which the information at
the URI specified by
uri
can be read. This stream must be
closed after reading is completed.
public Stream OpenWrite(string
uri
)
Returns an output stream to which information can be
written to the URI specified by
uri.
This stream must be
closed after writing is completed.
public Stream OpenWrite(Uri
uri
)
Returns an output stream to which information can be
written to the URI specified by
uri.
This stream must be
closed after writing is completed.
public Stream OpenWrite(string
uri
, string
how
)
Returns an output stream to which information can be
written to the URI specified by
uri.
This stream must be
closed after writing is completed. The string passed in
how
specifies how the information will be written.
public Stream OpenWrite(Uri
uri
, string
how
)
Returns an output stream to which information can be
written to the URI specified by
uri.
This stream must be
closed after writing is completed. The string passed in
how
specifies how the information will be written.
public byte[ ]
UploadData(string
uri
, byte[ ]
info
)
Writes the information specified by
info
to the URI specified
by
uri.
The response is returned.
public byte[ ]
UploadData(Uri
uri
, byte[ ]
info
)
Writes the information specified by
info
to the URI specified
by
uri.
The response is returned.
public byte[ ]
UploadData(string
uri
,
string
how
,
byte[ ]
info
)
Writes the information specified by
info
to the URI specified
by
uri.
The response is returned. The string passed in
how
specifies how the information will be written.
T
ABLE
25-7
The Synchronous Methods Defi ned by
WebClient
This program downloads the information at McGrawHill.com and puts it into a file called
Do'stlaringiz bilan baham: |