WebClient クラス
アセンブリ: System (system.dll 内)
構文
解説
WebClient クラスは、URI で識別されるローカル、イントラネット、またはインターネットの各リソースとのデータの送受信用の共通のメソッドを提供します。
WebClient クラスは、WebRequest クラスを使用してリソースにアクセスできるようにします。WebClient インスタンスは、WebRequest.RegisterPrefix メソッドで登録した WebRequest の子孫を使用してデータにアクセスできます。
メモ |
---|
既定では、.NET Framework は、http:、https:、ftp:、および file: の各スキーム識別子で始まる URI をサポートします。 |
リソースにデータをアップロードするための WebClient のメソッドを次の表に示します。
OpenWrite | |
OpenWriteAsync | |
UploadData | |
UploadDataAsync | |
UploadFile | |
UploadFileAsync | |
UploadValues | |
UploadValuesAsync | 呼び出し元のスレッドをブロックせずに、リソースに NameValueCollection を送信し、任意の応答を含む Byte 配列を返します。 |
UploadString | |
UploadStringAsync |
リソースからデータをダウンロードするための WebClient のメソッドを次の表に示します。
OpenRead | |
OpenReadAsync | |
DownloadData | |
DownloadDataAsync | |
DownloadFile | |
DownloadFileAsync | |
DownloadString | |
DownloadStringAsync |
CancelAsync メソッドを使用すると、未完了の非同期操作をキャンセルできます。
WebClient インスタンスは、既定では省略可能な HTTP ヘッダーを送信しません。要求に省略可能なヘッダーを使用する場合は、そのヘッダーを Headers コレクションに追加する必要があります。たとえば、応答にクエリを保持するには、ユーザー エージェント ヘッダーを追加する必要があります。また、ユーザー エージェント ヘッダーがない場合には、サーバーは 500 (内部サーバー エラー) を返す場合があります。
WebClient インスタンスの AllowAutoRedirect は true に設定されています。
継承時の注意 派生クラスは、WebClient の基本クラスの実装を呼び出して、派生クラスが予想どおりに動作できるようにする必要があります。リソースの 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.MarshalByRefObject
System.ComponentModel.Component
System.Net.WebClient
プラットフォーム
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- WebClient クラスのページへのリンク