WebResponse.ContentType 属性

当在派生类中重写时,获取或设置接收的数据的内容类型。

**命名空间:**System.Net
**程序集:**System(在 system.dll 中)

语法

声明
Public Overridable Property ContentType As String
用法
Dim instance As WebResponse
Dim value As String

value = instance.ContentType

instance.ContentType = value
public virtual string ContentType { get; set; }
public:
virtual property String^ ContentType {
    String^ get ();
    void set (String^ value);
}
/** @property */
public String get_ContentType ()

/** @property */
public void set_ContentType (String value)
public function get ContentType () : String

public function set ContentType (value : String)

属性值

包含响应的内容类型的字符串。

异常

异常类型 条件

NotSupportedException

当未在子类中重写该属性时,试图获取或设置该属性。

备注

如果已知,则 ContentType 属性包含 Internet 资源响应的 MIME 内容类型。

提示

WebResponse 类是一个 abstract 类。WebResponse 实例在运行时的实际行为由 WebRequest.GetResponse 返回的子类确定。有关默认值和异常的更多信息,请参见有关子类的文档,例如 HttpWebResponseFileWebResponse

示例

下面的示例使用 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()
         // 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(); 
// 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();
// 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}", 
    System.Convert.ToString(myWebResponse.get_ContentLength()),
    System.Convert.ToString(myWebResponse.get_ContentType()));
// Release resources of response object.
myWebResponse.Close();

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

WebResponse 类
WebResponse 成员
System.Net 命名空间
HttpWebResponse.ContentType 属性