WebRequest.ContentType 屬性

定義

在子代類別中覆寫時,取得或設定正在傳送要求資料的內容類型。

public:
 abstract property System::String ^ ContentType { System::String ^ get(); void set(System::String ^ value); };
public:
 virtual property System::String ^ ContentType { System::String ^ get(); void set(System::String ^ value); };
public abstract string ContentType { get; set; }
public virtual string? ContentType { get; set; }
public virtual string ContentType { get; set; }
member this.ContentType : string with get, set
Public MustOverride Property ContentType As String
Public Overridable Property ContentType As String

屬性值

String

要求資料的內容類型。

例外狀況

當屬性在子代類別中未覆寫時,會嘗試取得或設定該屬性。

範例

下列範例會將 ContentType 屬性設定為適當的媒體類型。

// Set the 'ContentType' property of the WebRequest.
myWebRequest->ContentType = "application/x-www-form-urlencoded";

// Set the 'ContentLength' property of the WebRequest.
myWebRequest->ContentLength = byteArray->Length;
Stream^ newStream = myWebRequest->GetRequestStream();
newStream->Write( byteArray, 0, byteArray->Length );

// Close the Stream object.
newStream->Close();

// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse^ myWebResponse = myWebRequest->GetResponse();

// Set the 'ContentType' property of the WebRequest.
myWebRequest.ContentType="application/x-www-form-urlencoded";

// Set the 'ContentLength' property of the WebRequest.
myWebRequest.ContentLength=byteArray.Length;
Stream newStream=myWebRequest.GetRequestStream();
newStream.Write(byteArray,0,byteArray.Length);

// Close the Stream object.
newStream.Close();

// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse=myWebRequest.GetResponse();


' Set the 'ContentType' property of the WebRequest.
myWebRequest.ContentType = "application/x-www-form-urlencoded"

' Set the 'ContentLength' property of the WebRequest.
myWebRequest.ContentLength = byteArray.Length
Dim newStream As Stream = myWebRequest.GetRequestStream()
newStream.Write(byteArray, 0, byteArray.Length)

' Close the Stream object.
newStream.Close()

' Assign the response object of 'WebRequest' to a 'WebResponse' variable.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

備註

屬性 ContentType 包含要求的媒體類型。 這通常是內容的 MIME 編碼方式。

注意

類別 WebRequest 是類別 abstract 。 執行時間實例的實際行為是由 方法所 WebRequest.Create 傳回的 WebRequest 子系類別所決定。 如需預設值和例外狀況的詳細資訊,請參閱 子系類別的檔,例如 HttpWebRequestFileWebRequest

適用於

另請參閱