WebResponse.ContentType Egenskap

Definition

När du åsidosättas i en härledd klass hämtar eller anger du innehållstypen för de data som tas emot.

public:
 abstract property System::String ^ ContentType { System::String ^ get(); };
public:
 virtual property System::String ^ ContentType { System::String ^ get(); void set(System::String ^ value); };
public abstract string ContentType { get; }
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

Egenskapsvärde

En sträng som innehåller svarets innehållstyp.

Undantag

Alla försök görs att hämta eller ange egenskapen när egenskapen inte åsidosättas i en underordnad klass.

Exempel

I följande exempel används ContentType egenskapen för att hämta innehållstypen för svaret.


         // 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()

Kommentarer

Egenskapen ContentType innehåller MIME-innehållstypen för svaret från Internetresursen, om det är känt.

Note

Klassen WebResponse är en abstract klass. Det faktiska beteendet för WebResponse instanser vid körning bestäms av den underordnade klassen som returneras av WebRequest.GetResponse. Mer information om standardvärden och undantag finns i dokumentationen för de underordnade klasserna, till exempel HttpWebResponse och FileWebResponse.

Gäller för

Se även