Bagikan melalui


WebClient.BaseAddress Properti

Definisi

Mendapatkan atau mengatur URI dasar untuk permintaan yang WebClientdibuat oleh .

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

Nilai Properti

yang String berisi URI dasar untuk permintaan yang WebClient dibuat oleh atau Empty jika tidak ada alamat dasar yang ditentukan.

Pengecualian

BaseAddress diatur ke URI yang tidak valid. Pengecualian dalam mungkin berisi informasi yang akan membantu Anda menemukan kesalahan.

Contoh

Contoh kode berikut mengunduh data dari server Internet dan menampilkannya di konsol. Ini mengasumsikan bahwa alamat server (seperti http://www.contoso.com) ada di hostUri dan bahwa jalur ke sumber daya (seperti /default.htm) berada di 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."))

Keterangan

Properti BaseAddress berisi URI dasar yang dikombinasikan dengan alamat relatif. Saat Anda memanggil metode yang mengunggah atau mengunduh data, WebClient objek menggabungkan URI dasar ini dengan alamat relatif yang Anda tentukan dalam panggilan metode. Jika Anda menentukan URI absolut, WebClient tidak menggunakan BaseAddress nilai properti.

Untuk menghapus nilai yang ditetapkan sebelumnya, atur properti ini ke null atau string kosong ("").

Berlaku untuk