WebClient クラスとは? わかりやすく解説

WebClient クラス

URI識別されるリソースとのデータの送受信用の共通のメソッド提供します

名前空間: System.Net
アセンブリ: System (system.dll 内)
構文構文

<ComVisibleAttribute(True)> _
Public Class WebClient
    Inherits Component
[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 インスタンスは、WebRequest.RegisterPrefix メソッド登録した WebRequest の子孫を使用してデータアクセスできます

メモメモ

既定では、.NET Framework は、http:https:ftp:、および file: の各スキーム識別子で始まる URIサポートします

リソースデータアップロードするための WebClientメソッド次の表に示します

リソースからデータダウンロードするための WebClientメソッド次の表に示します

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();
}

.NET Framework のセキュリティ.NET Frameworkセキュリティ
継承階層継承階層
System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
      System.Net.WebClient
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
WebClient メンバ
System.Net 名前空間
WebRequest
WebResponse
HttpWebRequest クラス
HttpWebResponse クラス


このページでは「.NET Framework クラス ライブラリ リファレンス」からWebClient クラスを検索した結果を表示しています。
Weblioに収録されているすべての辞書からWebClient クラスを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からWebClient クラスを検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「WebClient クラス」の関連用語

WebClient クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



WebClient クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS