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。 内部异常可能包含有助于找到错误的信息。

示例

下面的代码示例从 Internet 服务器下载数据并将其显示在主机上。 它假定服务器的地址 ((如 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."))

注解

属性 BaseAddress 包含与相对地址组合的基 URI。 调用上传或下载数据的方法时, WebClient 对象将此基 URI 与你在方法调用中指定的相对地址组合在一起。 如果指定绝对 URI, WebClient 不使用 BaseAddress 属性值。

若要删除以前设置的值,请将此属性设置为 null 或空字符串 (“”) 。

适用于