WebClient.BaseAddress Property

Definition

Gets or sets the base URI for requests made by a WebClient.

C#
public string BaseAddress { get; set; }

Property Value

A String containing the base URI for requests made by a WebClient or Empty if no base address has been specified.

Exceptions

BaseAddress is set to an invalid URI. The inner exception may contain information that will help you locate the error.

Examples

The following code example downloads data from an Internet server and displays it on the console. It assumes that the server's address (such as http://www.contoso.com) is in hostUri and that the path to the resource (such as /default.htm) is in uriSuffix.

C#
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

// Set the BaseAddress of the Web Resource in the WebClient.
myWebClient.BaseAddress = hostUri;
Console.WriteLine("Downloading from " + hostUri + "/" + uriSuffix);
Console.WriteLine("\nPress Enter key to continue");
Console.ReadLine();	

// Download the target Web Resource into a byte array.
byte[] myDatabuffer = myWebClient.DownloadData (uriSuffix);

// Display the downloaded data.
string download = Encoding.ASCII.GetString(myDatabuffer);
Console.WriteLine(download);

Console.WriteLine("Download of " + myWebClient.BaseAddress.ToString() + uriSuffix + " was successful.");

Remarks

Výstraha

WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete, and you shouldn't use them for new development. Use HttpClient instead.

The BaseAddress property contains a base URI that is combined with a relative address. When you call a method that uploads or downloads data, the WebClient object combines this base URI with the relative address you specify in the method call. If you specify an absolute URI, WebClient does not use the BaseAddress property value.

To remove a previously set value, set this property to null or an empty string ("").

Applies to

Produkt Verzie
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1