WebClient.BaseAddress Proprietà

Definizione

Recupera o imposta l'URI di base per richieste effettuate da un oggetto 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

Valore della proprietà

Classe String contenente l'URI di base per le richieste fatte da una classe WebClient o da un campo Empty nel caso non sia stato specificato l'indirizzo di base.

Eccezioni

BaseAddress è impostato su un URI non valido. L'eccezione interna può contenere le informazioni utili per individuare l'errore.

Esempio

L'esempio di codice seguente scarica i dati da un server Internet e lo visualizza nella console. Si presuppone che l'indirizzo del server (ad esempio http://www.contoso.com) sia in hostUri e che il percorso della risorsa (ad esempio /default.htm) sia 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."))

Commenti

La BaseAddress proprietà contiene un URI di base combinato con un indirizzo relativo. Quando si chiama un metodo che carica o scarica i dati, l'oggetto WebClient combina questo URI di base con l'indirizzo relativo specificato nella chiamata al metodo. Se si specifica un URI assoluto, WebClient non usa il valore della BaseAddress proprietà.

Per rimuovere un valore impostato in precedenza, impostare questa proprietà su null o su una stringa vuota ("").

Si applica a