按一下以給予評分及指教
MSDN
MSDN Library
.NET 開發
先前版本
類別庫參考
System.Net
WebClient 類別

  開啟低頻寬檢視
本頁僅適用於
Microsoft Visual Studio 2005/.NET Framework 2.0

其他版本也適用於下列軟體:
.NET Framework 類別庫
WebClient 類別

提供從 URI 識別的資源處傳送和接收資料的通用方法。

命名空間: System.Net
組件: System (在 system.dll 中)

Visual Basic (宣告)
<ComVisibleAttribute(True)> _
Public Class WebClient
    Inherits Component
Visual Basic (使用方式)
Dim instance As WebClient
C#
[ComVisibleAttribute(true)] 
public class WebClient : Component
C++
[ComVisibleAttribute(true)] 
public ref class WebClient : public Component
J#
/** @attribute ComVisibleAttribute(true) */ 
public class WebClient extends Component
JScript
ComVisibleAttribute(true) 
public class WebClient extends Component

WebClient 類別提供從 URI 識別的任何本機、內部網路或網際網路資源處傳送和接收資料的通用方法。

WebClient 類別使用 WebRequest 類別提供資源的存取權。WebClient 執行個體 (Instance) 可以使用任何以 WebRequest.RegisterPrefix 方法註冊的 WebRequest 子代 (Descendant) 來存取資料。

Note注意事項

根據預設,.NET Framework 支援以 http:https:、ftp: 和 file: 配置識別項開頭的 URI。

下表描述用於將資料上載至資源的 WebClient 方法。

方法

說明

OpenWrite

擷取用於將資料傳送至資源的 Stream

OpenWriteAsync

擷取用於將資料傳送至資源 Stream,而不封鎖呼叫的執行緒。

UploadData

將位元組陣列傳送至資源,並傳回含有任何回應的 Byte 陣列。

UploadDataAsync

Byte 陣列傳送至資源,而不封鎖呼叫的執行緒。

UploadFile

將本機檔案傳送至資源,並傳回含有任何回應的 Byte 陣列。

UploadFileAsync

將本機檔案傳送至資源,而不封鎖呼叫的執行緒。

UploadValues

NameValueCollection 傳送至資源,並傳回含有任何回應的 Byte 陣列。

UploadValuesAsync

NameValueCollection 傳送至資源,並傳回含有任何回應的 Byte 陣列,而不封鎖呼叫的執行緒。

UploadString

String 傳送至資源,而不封鎖呼叫的執行緒。

UploadStringAsync

String 傳送至資源,而不封鎖呼叫的執行緒。

下表描述用於從資源下載資料的 WebClient 方法。

方法

說明

OpenRead

從資源將資料做為 Stream 傳回。

OpenReadAsync

從資源傳回資料,而不封鎖呼叫的執行緒。

DownloadData

從資源下載資料並傳回 Byte 陣列。

DownloadDataAsync

從資源下載資料,並傳回 Byte 陣列,而不封鎖呼叫的執行緒。

DownloadFile

從資源下載資料至本機檔案。

DownloadFileAsync

從資源下載資料至本機檔案,而不封鎖呼叫的執行緒。

DownloadString

從資源下載 String,並傳回 String

DownloadStringAsync

從資源下載 String,而不封鎖呼叫的執行緒。

您可以使用 CancelAsync 方法取消尚未完成的非同步作業。

依照預設值,WebClient 執行個體並不會傳送選擇性的 HTTP 標頭。如果您的要求需要有選擇性的標頭,您就必須將標頭加入至 Headers 集合中。例如,若要在回應中保留查詢,您就必須加入使用者代理程式 (User-Agent) 的標頭。同時,如果遺失使用者代理程式標頭,則伺服器可能傳回 500 (內部伺服器錯誤)。

WebClient 執行個體中的 AllowAutoRedirect 設為 true

繼承者注意事項 衍生類別 (Derived Class) 應呼叫 WebClient 的基底類別 (Base Class) 實作,以確保衍生類別可如預期般執行。

下列程式碼範例會取得資源的 URI、擷取其值,並顯示回應。

Visual Basic
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
C#
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 ();
    }
}
C++
#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();
}

  • WebPermission  - 用於存取所要求的 URI,或是所有重新導向要求的 URI。關聯的列舉型別:Connect
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
社群內容   什麼是社群內容?
新增內容 RSS  註解
Processing
© 2009 Microsoft Corporation. 著作權所有,並保留一切權利。 使用規定  |  商標  |  隱私權聲明
Page view tracker