WebResponse.ContentType 屬性
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在衍生類別中覆寫時,取得或設定正在接收資料的內容類型。
public:
abstract property System::String ^ ContentType { System::String ^ get(); };
public:
virtual property System::String ^ ContentType { System::String ^ get(); void set(System::String ^ value); };
C#
public abstract string ContentType { get; }
C#
public virtual string ContentType { get; set; }
member this.ContentType : string
member this.ContentType : string with get, set
Public MustOverride ReadOnly Property ContentType As String
Public Overridable Property ContentType As String
字串,包含回應的內容類型。
當屬性在子代類別中未覆寫時,會嘗試取得或設定該屬性。
下列範例會 ContentType 使用 屬性來取得回應的內容類型。
// Create a 'WebRequest' with the specified url.
WebRequest^ myWebRequest = WebRequest::Create( "http://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("http://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.
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()
如果已知,此屬性 ContentType 包含來自因特網資源的回應MIME內容類型。
備註
類別 WebResponse 是類別 abstract
。 運行時間實例的實際行為是由 所WebRequest.GetResponse傳回的WebResponse子系類別所決定。 如需預設值和例外狀況的詳細資訊,請參閱 子系類別的檔,例如 HttpWebResponse 和 FileWebResponse。
產品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
UWP | 10.0 |