提供從 URI 識別的資源處傳送和接收資料的通用方法。
命名空間: System.Net
組件: System (在 system.dll 中)
<ComVisibleAttribute(True)> _
Public Class WebClient
Inherits Component
Dim instance As WebClient
[ComVisibleAttribute(true)]
public class WebClient : Component
[ComVisibleAttribute(true)]
public ref class WebClient : public Component
/** @attribute ComVisibleAttribute(true) */
public class WebClient extends Component
ComVisibleAttribute(true)
public class WebClient extends Component
WebClient 類別提供從 URI 識別的任何本機、內部網路或網際網路資源處傳送和接收資料的通用方法。
WebClient 類別使用 WebRequest 類別提供資源的存取權。WebClient 執行個體 (Instance) 可以使用任何以 WebRequest.RegisterPrefix 方法註冊的 WebRequest 子代 (Descendant) 來存取資料。
注意事項 |
|---|
| 根據預設,.NET Framework 支援以 http:、https:、ftp: 和 file: 配置識別項開頭的 URI。 |
下表描述用於將資料上載至資源的 WebClient 方法。
下表描述用於從資源下載資料的 WebClient 方法。
您可以使用 CancelAsync 方法取消尚未完成的非同步作業。
依照預設值,WebClient 執行個體並不會傳送選擇性的 HTTP 標頭。如果您的要求需要有選擇性的標頭,您就必須將標頭加入至 Headers 集合中。例如,若要在回應中保留查詢,您就必須加入使用者代理程式 (User-Agent) 的標頭。同時,如果遺失使用者代理程式標頭,則伺服器可能傳回 500 (內部伺服器錯誤)。
WebClient 執行個體中的 AllowAutoRedirect 設為 true。
繼承者注意事項
衍生類別 (Derived Class) 應呼叫 WebClient 的基底類別 (Base Class) 實作,以確保衍生類別可如預期般執行。
下列程式碼範例會取得資源的 URI、擷取其值,並顯示回應。
Imports System
Imports System.Net
Imports System.IO
Public Class Test
Public Shared Sub Main(args() As String)
If args Is Nothing OrElse args.Length = 0 Then
Throw New ApplicationException("Specify the URI of the resource to retrieve.")
End If
Dim client As New WebClient()
' Add a user agent header in case the
' requested URI contains a query.
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")
Dim data As Stream = client.OpenRead(args(0))
Dim reader As New StreamReader(data)
Dim s As String = reader.ReadToEnd()
Console.WriteLine(s)
data.Close()
reader.Close()
End Sub 'Main
End Class 'Test
using System;
using System.Net;
using System.IO;
public class Test
{
public static void Main (string[] args)
{
if (args == null || args.Length == 0)
{
throw new ApplicationException ("Specify the URI of the resource to retrieve.");
}
WebClient client = new WebClient ();
// Add a user agent header in case the
// requested URI contains a query.
client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream data = client.OpenRead (args[0]);
StreamReader reader = new StreamReader (data);
string s = reader.ReadToEnd ();
Console.WriteLine (s);
data.Close ();
reader.Close ();
}
}
#using <System.dll>
using namespace System;
using namespace System::Net;
using namespace System::IO;
int main()
{
array<String^>^args = Environment::GetCommandLineArgs();
if ( args == nullptr || args->Length == 1 )
{
throw gcnew ApplicationException( "Specify the URI of the resource to retrieve." );
}
WebClient^ client = gcnew WebClient;
// Add a user agent header in case the
// requested URI contains a query.
client->Headers->Add( "user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)" );
Stream^ data = client->OpenRead( args[ 1 ] );
StreamReader^ reader = gcnew StreamReader( data );
String^ s = reader->ReadToEnd();
Console::WriteLine( s );
data->Close();
reader->Close();
}
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Net.WebClient
這個型別的所有公用靜態成員 (即 Visual Basic 中的 Shared 成員) 都是安全執行緒。並非所有的執行個體成員均為安全執行緒。
Windows 98、 Windows 2000 SP4、 Windows Millennium Edition、 Windows Server 2003、 Windows XP Media Center Edition、 Windows XP Professional x64 Edition、 Windows XP SP2、 Windows XP Starter Edition
.NET Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱系統需求一節的內容。
.NET Framework
支援版本:2.0、1.1、1.0