共用方式為


WebClient.BaseAddress 屬性

定義

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

public:
 property System::String ^ BaseAddress { System::String ^ get(); void set(System::String ^ value); };
public string BaseAddress { get; set; }
member this.BaseAddress : string with get, set
Public Property BaseAddress As String

屬性值

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

例外狀況

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

範例

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

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;

// Set the BaseAddress of the Web Resource in the WebClient.
myWebClient->BaseAddress = hostUri;
Console::WriteLine( "Downloading from {0}/ {1}", hostUri, uriSuffix );
Console::WriteLine( "\nPress Enter key to continue" );
Console::ReadLine();

// Download the target Web Resource into a Byte array.
array<Byte>^ myDatabuffer = myWebClient->DownloadData( uriSuffix );

// Display the downloaded data.
String^ download = Encoding::ASCII->GetString( myDatabuffer );
Console::WriteLine( download );
Console::WriteLine( "Download of {0}{1} was successful.", myWebClient->BaseAddress, uriSuffix );
// 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.");
    ' Create a new WebClient instance.
    Dim myWebClient As New WebClient()

    ' Set the BaseAddress of the Web resource in the WebClient.
    myWebClient.BaseAddress = hostUri
    Console.WriteLine(("Downloading from " + hostUri + "/" + uriSuffix))
    Console.WriteLine(ControlChars.Cr + "Press Enter key to continue")
    Console.ReadLine()

    ' Download the target Web resource into a byte array.
    Dim myDatabuffer As Byte() = myWebClient.DownloadData(uriSuffix)

    ' Display the downloaded data.
Dim download As String = 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 或空字串 (“” )。

適用於