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

WebRequest クラス

URI (Uniform Resource Identifier) への要求行います。これは abstract クラスです。

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

<SerializableAttribute> _
Public MustInherit Class
 WebRequest
    Inherits MarshalByRefObject
    Implements ISerializable
[SerializableAttribute] 
public abstract class WebRequest : MarshalByRefObject,
 ISerializable
[SerializableAttribute] 
public ref class WebRequest abstract : public
 MarshalByRefObject, ISerializable
/** @attribute SerializableAttribute() */ 
public abstract class WebRequest extends MarshalByRefObject
 implements ISerializable
SerializableAttribute 
public abstract class WebRequest extends
 MarshalByRefObject implements ISerializable
解説解説

WebRequest は、インターネットからのデータアクセスするための .NET Framework要求/応答モデル用の abstract 基本クラスです。要求/応答モデル使用するアプリケーションは、プロトコル寛容方法で、インターネットからデータ要求できます。この場合アプリケーションWebRequest クラスインスタンス動作しプロトコル固有派生クラス要求詳細実行します

要求は、アプリケーションからサーバー上の Web ページなどの特定の URI送信されます。URI は、そのアプリケーション用に登録されている WebRequest 派生クラスの一覧から、作成する適切な派生クラス決定します通常WebRequest 派生クラスは、HTTP または FTP などの固有のプロトコル処理するために登録されていますが、固有サーバーまたはサーバーパスへの要求処理するために登録することもできます

WebRequest クラスは、インターネット リソースへのアクセス中にエラー発生したときに WebException をスローます。WebException.Status プロパティは、エラー原因を示す WebExceptionStatus 値の 1 つです。WebException.Status が WebExceptionStatus.ProtocolError の場合Response プロパティには、インターネット リソースから受信した WebResponse が格納されます。

WebRequest クラスabstract クラスであるため、実行時WebRequest インスタンス実際動作は、System.Net.WebRequest.Create メソッド返される派生クラスによって決まります既定値および例外詳細については、HttpWebRequest や FileWebRequest などの派生クラス説明参照してください

メモメモ

新しWebRequest インスタンス初期化するには、Create メソッド使用しますWebRequest コンストラクタ使用しないください

継承時の注意 WebRequest から継承する場合は、Method、RequestUri、Headers、ContentLength、ContentTypeCredentials、PreAuthenticate、GetRequestStream、BeginGetRequestStream、EndGetRequestStream、GetResponse、BeginGetResponse、EndGetResponse の各メンバオーバーライドする必要があります。さらに、IWebRequestCreate インターフェイス実装指定します。これは、Create呼び出すときに使用する Create メソッド定義しますまた、RegisterPrefix メソッドまたは構成ファイル使用してIWebRequestCreate インターフェイス実装するクラス登録する必要もあります

使用例使用例

WebRequest インスタンス作成し応答返す方法次の例に示します

Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Namespace Examples.System.Net
    Public Class WebRequestGetExample

        Public Shared Sub
 Main()
            ' Create a request for the URL.         
            Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/default.html")
            ' If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials
            ' Get the response.
            Dim response As HttpWebResponse
 = CType(request.GetResponse(), HttpWebResponse)
            ' Display the status.
            Console.WriteLine(response.StatusDescription)
            ' Get the stream containing content returned by the server.
            Dim dataStream As Stream = response.GetResponseStream()
            ' Open the stream using a StreamReader for easy access.
            Dim reader As New
 StreamReader(dataStream)
            ' Read the content.
            Dim responseFromServer As String
 = reader.ReadToEnd()
            ' Display the content.
            Console.WriteLine(responseFromServer)
            ' Cleanup the streams and the response.
            reader.Close()
            dataStream.Close()
            response.Close()
        End Sub 'Main
    End Class 'WebRequestGetExample
End Namespace
using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void
 Main ()
        {
            // Create a request for the URL.         
            WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html");
            // If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
            // Display the status.
            Console.WriteLine (response.StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream ();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd ();
            // Display the content.
            Console.WriteLine (responseFromServer);
            // Cleanup the streams and the response.
            reader.Close ();
            dataStream.Close ();
            response.Close ();
        }
    }
}
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Net;
using namespace System::Text;
int main()
{
   
   // Create a request for the URL.   
   WebRequest^ request = WebRequest::Create( "http://www.contoso.com/default.html"
 );
   
   // If required by the server, set the credentials.
   request->Credentials = CredentialCache::DefaultCredentials;
   
   // Get the response.
   HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
   
   // Display the status.
   Console::WriteLine( response->StatusDescription );
   
   // Get the stream containing content returned by the server.
   Stream^ dataStream = response->GetResponseStream();
   
   // Open the stream using a StreamReader for easy access.
   StreamReader^ reader = gcnew StreamReader( dataStream );
   
   // Read the content.
   String^ responseFromServer = reader->ReadToEnd();
   
   // Display the content.
   Console::WriteLine( responseFromServer );
   
   // Cleanup the streams and the response.
   reader->Close();
   dataStream->Close();
   response->Close();
}

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


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

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

辞書ショートカット

すべての辞書の索引

「WebRequest クラス」の関連用語

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

   

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



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

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

©2025 GRAS Group, Inc.RSS