次の方法で共有


WebResponse.ContentType プロパティ

派生クラスでオーバーライドされると、受信しているデータのコンテンツ タイプを取得または設定します。

Public Overridable Property ContentType As String
[C#]
public virtual string ContentType {get; set;}
[C++]
public: __property virtual String* get_ContentType();public: __property virtual void set_ContentType(String*);
[JScript]
public function get ContentType() : String;public function set ContentType(String);

プロパティ値

応答のコンテンツ タイプを格納する文字列。

例外

例外の種類 条件
NotSupportedException プロパティが派生クラスでオーバーライドされていないのに、そのプロパティの取得または設定が試行されました。

解説

ContentType プロパティは、インターネット リソースからの応答の MIME コンテンツ タイプが確認された場合に、その値を格納します。

メモ    WebResponse クラスは、抽象 (Visual Basic では MustInherit) クラスです。実行時の WebResponse インスタンスの実際の動作は、 WebRequest.GetResponse で返される派生クラスによって決まります。既定値および例外の詳細については、 HttpWebResponseFileWebResponse などの派生クラスの説明を参照してください。

使用例

[Visual Basic, C#, C++] ContentType プロパティを使用して、応答のコンテンツ タイプを取得する例を次に示します。

 

' Create a 'WebRequest' with the specified url.     
Dim myWebRequest As WebRequest = WebRequest.Create("www.contoso.com")

' Send the 'WebRequest' and wait for response.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

' The ContentLength and ContentType received as headers in the response object are also exposed as properties.
   ' These provide information about the length and type of the entity body in the response.
Console.WriteLine(ControlChars.Cr + "Content length :{0}, Content Type : {1}", myWebResponse.ContentLength, myWebResponse.ContentType)
myWebResponse.Close()


[C#] 

                     // Create a 'WebRequest' with the specified url.
            WebRequest myWebRequest = WebRequest.Create("https://www.contoso.com"); 

            // Send the 'WebRequest' and wait for response.
            WebResponse myWebResponse = myWebRequest.GetResponse(); 

            // Display the content length and content type received as headers in the response object.
            Console.WriteLine("\nContent length :{0}, Content Type : {1}", 
                                         myWebResponse.ContentLength, 
                                         myWebResponse.ContentType);  
            
            // Release resources of response object.
            myWebResponse.Close(); 
            

[C++] 
// Create a 'WebRequest' with the specified url.
WebRequest* myWebRequest = WebRequest::Create(S"https://www.contoso.com");

// Send the 'WebRequest' and wait for response.
WebResponse* myWebResponse = myWebRequest->GetResponse();

// Display the content length and content type received as headers in the response object.
Console::WriteLine(S"\nContent length : {0}, Content Type : {1}", __box(myWebResponse->ContentLength),
   myWebResponse->ContentType);

// Release resources of response object.
myWebResponse->Close();

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

WebResponse クラス | WebResponse メンバ | System.Net 名前空間 | HttpWebResponse.ContentType