WebClient.BaseAddress Property

Definition

取得或設定由 WebClient 要求的基底 URI。Gets or sets the base URI for requests made by a WebClient.

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

Property Value

String

String,包含 WebClient 所提出之要求的基底 URI;如果未指定基底位址 (Base Address),則為 EmptyA String containing the base URI for requests made by a WebClient or Empty if no base address has been specified.

Exceptions

BaseAddress 是設為無效的 URI。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. 它假設伺服器的位址(例如 http://www.contoso.com) 是在 hostUri 中,而資源的路徑(例如/default.htm)是在 uriSuffix中。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.

// 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."))

Remarks

BaseAddress 屬性包含與相對位址結合的基底 URI。The BaseAddress property contains a base URI that is combined with a relative address. 當您呼叫上傳或下載資料的方法時,WebClient 物件會將此基底 URI 與您在方法呼叫中指定的相對位址結合。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. 如果您指定絕對 URI,WebClient 不會使用 BaseAddress 屬性值。If you specify an absolute URI, WebClient does not use the BaseAddress property value.

若要移除先前設定的值,請將此屬性設定為 null 或空字串("")。To remove a previously set value, set this property to null or an empty string ("").

Applies to