WebClient.BaseAddress 屬性

定義

取得或設定 WebClient所提出要求的基底 URI。

C#
public string BaseAddress { get; set; }

屬性值

String,包含 WebClient 所提出要求的基底 URI,如果未指定任何基位址,則為 Empty

例外狀況

BaseAddress 設定為無效的 URI。 內部例外狀況可能包含可協助您找出錯誤的資訊。

範例

下列程式代碼範例會從因特網伺服器下載數據,並在控制台上顯示。 它假設伺服器的位址 (例如 http://www.contoso.com) 位於 hostUri,且資源的路徑 (例如 /default.htm) 位於 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.");

備註

警告

WebRequestHttpWebRequestServicePointWebClient 已經過時,您不應該將它們用於新的開發。 請改用 HttpClient

BaseAddress 屬性包含與相對位址結合的基底 URI。 當您呼叫上傳或下載數據的方法時,WebClient 物件會將這個基底 URI 與您在方法呼叫中指定的相對地址結合。 如果您指定絕對 URI,WebClient 不會使用 BaseAddress 屬性值。

若要移除先前設定的值,請將此屬性設定為 null 或空字串 (“” )。

適用於

產品 版本
.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