WebClient イベント

名前 | 説明 | |
---|---|---|
![]() | Disposed | コンポーネントの Disposed イベントを待機するイベント ハンドラを追加します。 ( Component から継承されます。) |
![]() | DownloadDataCompleted | 非同期のデータ ダウンロード操作の完了時に発生します。 |
![]() | DownloadFileCompleted | 非同期のファイル ダウンロード操作の完了時に発生します。 |
![]() | DownloadProgressChanged | 非同期ダウンロード操作で、データの一部またはすべてが正常に転送された場合に発生します。 |
![]() | DownloadStringCompleted | 非同期のリソース ダウンロード操作の完了時に発生します。 |
![]() | OpenReadCompleted | リソースを格納しているストリームを開く非同期操作の完了時に発生します。 |
![]() | OpenWriteCompleted | リソースにデータを書き込むためにストリームを開く非同期操作の完了時に発生します。 |
![]() | UploadDataCompleted | 非同期のデータ アップロード操作の完了時に発生します。 |
![]() | UploadFileCompleted | 非同期のファイル アップロード操作の完了時に発生します。 |
![]() | UploadProgressChanged | 非同期アップロード操作で、データの一部またはすべてが正常に送信された場合に発生します。 |
![]() | UploadStringCompleted | 非同期の文字列アップロード操作の完了時に発生します。 |
![]() | UploadValuesCompleted | 名前/値コレクションの非同期アップロードの完了時に発生します。 |

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 コンストラクタ
アセンブリ: System (system.dll 内)


既定のコンストラクタは、WebClient クラスの新しいインスタンスを作成します。既定の HTTP メソッドは GET です。既定の FTP メソッドは RETR です。既定値 Encoding は Default です。AllowAutoRedirect の既定値は true です。

WebClient インスタンスを作成し、そのインスタンスを使用して、サーバーからデータをダウンロードしてシステム コンソールに表示し、サーバーからデータをダウンロードしてファイルに書き込み、サーバーにフォーム値をアップロードして応答を受信するコード例を次に示します。
Public Shared Sub Main() Try Dim client As New WebClient() Dim pageData As [Byte]() = client.DownloadData("http://www.contoso.com") Dim pageHtml As String = Encoding.ASCII.GetString(pageData) ' Download the data to a buffer. Console.WriteLine(pageHtml) ' Download the data to a file. client.DownloadFile("http://www.contoso.com", "page.htm") ' Upload some form post values. dim form as New NameValueCollection() form.Add("MyName", "MyValue") ' Note that you need to replace "http://localhost/somefile.aspx" with the name of ' a file that is available to your computer. Dim responseData As [Byte]() = client.UploadValues("http://www.contoso.com/form.aspx", form) Console.WriteLine(Encoding.ASCII.GetString(responseData)) Catch webEx As WebException if webEx.Status = WebExceptionStatus.ConnectFailure then Console.WriteLine("Are you behind a firewall? If so, go through the proxy server.") end if Console.Write(webEx.ToString()) End Try End Sub 'Main
try { // Download the data to a buffer. WebClient client = new WebClient(); Byte[] pageData = client.DownloadData("http://www.contoso.com"); string pageHtml = Encoding.ASCII.GetString(pageData); Console.WriteLine(pageHtml); // Download the data to a file. client.DownloadFile("http://www.contoso.com", "page.htm"); // Upload some form post values. NameValueCollection form = new NameValueCollection(); form.Add("MyName", "MyValue"); Byte[] responseData = client.UploadValues("http://www.contoso.com/form.aspx", form); } catch (WebException webEx) { Console.WriteLine(webEx.ToString()); if(webEx.Status == WebExceptionStatus.ConnectFailure) { Console.WriteLine("Are you behind a firewall? If so, go through the proxy server."); } }
try { // Download the data to a buffer. WebClient^ client = gcnew WebClient; array<Byte>^ pageData = client->DownloadData( "http://www.contoso.com" ); String^ pageHtml = Encoding::ASCII->GetString( pageData ); Console::WriteLine( pageHtml ); // Download the data to a file. client->DownloadFile( "http://www.contoso.com", "page.htm" ); // Upload some form post values. NameValueCollection^ form = gcnew NameValueCollection; form->Add( "MyName", "MyValue" ); array<Byte>^ responseData = client->UploadValues( "http://www.contoso.com/form.aspx", form ); } catch ( WebException^ webEx ) { Console::WriteLine( webEx->ToString() ); if ( webEx->Status == WebExceptionStatus::ConnectFailure ) { Console::WriteLine( "Are you behind a firewall? If so, go through the proxy server." ); } }
try { // Download the data to a buffer. WebClient client = new WebClient(); ubyte pageData[] = client.DownloadData("http://www.contoso.com"); String pageHtml = Encoding.get_ASCII().GetString(pageData); Console.WriteLine(pageHtml); // Download the data to a file. client.DownloadFile("http://www.contoso.com", "page.htm"); // Upload some form post values. NameValueCollection form = new NameValueCollection(); form.Add("MyName", "MyValue"); ubyte responseData[] = client.UploadValues( "http://www.contoso.com/form.aspx", form); } catch (WebException webEx) { Console.WriteLine(webEx.ToString()); if (webEx.get_Status().Equals(WebExceptionStatus.ConnectFailure)) { Console.WriteLine("Are you behind a firewall? If so, go" + " through the proxy server."); } }

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 プロパティ

名前 | 説明 | |
---|---|---|
![]() | BaseAddress | WebClient で作成された要求のベース URI を取得または設定します。 |
![]() | CachePolicy | WebRequest オブジェクトを使用してこの WebClient インスタンスが取得するリソースに対する、アプリケーションのキャッシュ ポリシーを取得または設定します。 |
![]() | Container | Component を格納している IContainer を取得します。 ( Component から継承されます。) |
![]() | Credentials | ホストに送信し、要求の認証に使用するネットワーク資格情報を取得または設定します。 |
![]() | Encoding | 文字列のアップロードとダウンロードに使用する Encoding を取得または設定します。 |
![]() | Headers | 要求に関連付けられているヘッダーの名前/値ペアのコレクションを取得または設定します。 |
![]() | IsBusy | Web 要求を処理中かどうかを取得します。 |
![]() | Proxy | この WebClient オブジェクトが使用するプロキシを取得または設定します。 |
![]() | QueryString | 要求に関連付けられているクエリの名前/値ペアのコレクションを取得または設定します。 |
![]() | ResponseHeaders | 応答に関連付けられているヘッダーの名前/値ペアのコレクションを取得します。 |
![]() | Site | Component の ISite を取得または設定します。 ( Component から継承されます。) |
![]() | UseDefaultCredentials | DefaultCredentials が要求と共に送信されるかどうかを制御する Boolean 値を取得または設定します。 |

名前 | 説明 | |
---|---|---|
![]() | CanRaiseEvents | コンポーネントがイベントを発生させることがきるかどうかを示す値を取得します。 ( Component から継承されます。) |
![]() | DesignMode | Component が現在デザイン モードかどうかを示す値を取得します。 ( Component から継承されます。) |
![]() | Events | Component に結び付けられているイベント ハンドラのリストを取得します。 ( Component から継承されます。) |

WebClient メソッド


名前 | 説明 | |
---|---|---|
![]() | Dispose | オーバーロードされます。 Component によって使用されているリソースを解放します。 ( Component から継承されます。) |
![]() | Finalize | Component がガベージ コレクションによってクリアされる前に、アンマネージ リソースを解放し、その他のクリーンアップ操作を実行します。 ( Component から継承されます。) |
![]() | GetService | Component またはその Container で提供されるサービスを表すオブジェクトを返します。 ( Component から継承されます。) |
![]() | GetWebRequest | 指定したリソースの WebRequest オブジェクトを返します。 |
![]() | GetWebResponse | オーバーロードされます。 指定した WebRequest の WebResponse を返します。 |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |
![]() | OnDownloadDataCompleted | DownloadDataCompleted イベントを発生させます。 |
![]() | OnDownloadFileCompleted | DownloadFileCompleted イベントを発生させます。 |
![]() | OnDownloadProgressChanged | DownloadProgressChanged イベントを発生させます。 |
![]() | OnDownloadStringCompleted | DownloadStringCompleted イベントを発生させます。 |
![]() | OnOpenReadCompleted | OpenReadCompleted イベントを発生させます。 |
![]() | OnOpenWriteCompleted | OpenWriteCompleted イベントを発生させます。 |
![]() | OnUploadDataCompleted | UploadDataCompleted イベントを発生させます。 |
![]() | OnUploadFileCompleted | UploadFileCompleted イベントを発生させます。 |
![]() | OnUploadProgressChanged | UploadProgressChanged イベントを発生させます。 |
![]() | OnUploadStringCompleted | UploadStringCompleted イベントを発生させます。 |
![]() | OnUploadValuesCompleted | UploadValuesCompleted イベントを発生させます。 |

WebClient メンバ
URI で識別されるリソースとのデータの送受信用の共通のメソッドを提供します。
WebClient データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | BaseAddress | WebClient で作成された要求のベース URI を取得または設定します。 |
![]() | CachePolicy | WebRequest オブジェクトを使用してこの WebClient インスタンスが取得するリソースに対する、アプリケーションのキャッシュ ポリシーを取得または設定します。 |
![]() | Container | Component を格納している IContainer を取得します。(Component から継承されます。) |
![]() | Credentials | ホストに送信し、要求の認証に使用するネットワーク資格情報を取得または設定します。 |
![]() | Encoding | 文字列のアップロードとダウンロードに使用する Encoding を取得または設定します。 |
![]() | Headers | 要求に関連付けられているヘッダーの名前/値ペアのコレクションを取得または設定します。 |
![]() | IsBusy | Web 要求を処理中かどうかを取得します。 |
![]() | Proxy | この WebClient オブジェクトが使用するプロキシを取得または設定します。 |
![]() | QueryString | 要求に関連付けられているクエリの名前/値ペアのコレクションを取得または設定します。 |
![]() | ResponseHeaders | 応答に関連付けられているヘッダーの名前/値ペアのコレクションを取得します。 |
![]() | Site | Component の ISite を取得または設定します。(Component から継承されます。) |
![]() | UseDefaultCredentials | DefaultCredentials が要求と共に送信されるかどうかを制御する Boolean 値を取得または設定します。 |

名前 | 説明 | |
---|---|---|
![]() | CanRaiseEvents | コンポーネントがイベントを発生させることがきるかどうかを示す値を取得します。(Component から継承されます。) |
![]() | DesignMode | Component が現在デザイン モードかどうかを示す値を取得します。(Component から継承されます。) |
![]() | Events | Component に結び付けられているイベント ハンドラのリストを取得します。(Component から継承されます。) |


名前 | 説明 | |
---|---|---|
![]() | Dispose | オーバーロードされます。 Component によって使用されているリソースを解放します。 (Component から継承されます。) |
![]() | Finalize | Component がガベージ コレクションによってクリアされる前に、アンマネージ リソースを解放し、その他のクリーンアップ操作を実行します。 (Component から継承されます。) |
![]() | GetService | Component またはその Container で提供されるサービスを表すオブジェクトを返します。 (Component から継承されます。) |
![]() | GetWebRequest | 指定したリソースの WebRequest オブジェクトを返します。 |
![]() | GetWebResponse | オーバーロードされます。 指定した WebRequest の WebResponse を返します。 |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |
![]() | OnDownloadDataCompleted | DownloadDataCompleted イベントを発生させます。 |
![]() | OnDownloadFileCompleted | DownloadFileCompleted イベントを発生させます。 |
![]() | OnDownloadProgressChanged | DownloadProgressChanged イベントを発生させます。 |
![]() | OnDownloadStringCompleted | DownloadStringCompleted イベントを発生させます。 |
![]() | OnOpenReadCompleted | OpenReadCompleted イベントを発生させます。 |
![]() | OnOpenWriteCompleted | OpenWriteCompleted イベントを発生させます。 |
![]() | OnUploadDataCompleted | UploadDataCompleted イベントを発生させます。 |
![]() | OnUploadFileCompleted | UploadFileCompleted イベントを発生させます。 |
![]() | OnUploadProgressChanged | UploadProgressChanged イベントを発生させます。 |
![]() | OnUploadStringCompleted | UploadStringCompleted イベントを発生させます。 |
![]() | OnUploadValuesCompleted | UploadValuesCompleted イベントを発生させます。 |

名前 | 説明 | |
---|---|---|
![]() | Disposed | コンポーネントの Disposed イベントを待機するイベント ハンドラを追加します。(Component から継承されます。) |
![]() | DownloadDataCompleted | 非同期のデータ ダウンロード操作の完了時に発生します。 |
![]() | DownloadFileCompleted | 非同期のファイル ダウンロード操作の完了時に発生します。 |
![]() | DownloadProgressChanged | 非同期ダウンロード操作で、データの一部またはすべてが正常に転送された場合に発生します。 |
![]() | DownloadStringCompleted | 非同期のリソース ダウンロード操作の完了時に発生します。 |
![]() | OpenReadCompleted | リソースを格納しているストリームを開く非同期操作の完了時に発生します。 |
![]() | OpenWriteCompleted | リソースにデータを書き込むためにストリームを開く非同期操作の完了時に発生します。 |
![]() | UploadDataCompleted | 非同期のデータ アップロード操作の完了時に発生します。 |
![]() | UploadFileCompleted | 非同期のファイル アップロード操作の完了時に発生します。 |
![]() | UploadProgressChanged | 非同期アップロード操作で、データの一部またはすべてが正常に送信された場合に発生します。 |
![]() | UploadStringCompleted | 非同期の文字列アップロード操作の完了時に発生します。 |
![]() | UploadValuesCompleted | 名前/値コレクションの非同期アップロードの完了時に発生します。 |

Weblioに収録されているすべての辞書からWebClientを検索する場合は、下記のリンクをクリックしてください。

- WebClientのページへのリンク