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.CreateWebRequest后代类确定。 有关默认值和异常的详细信息,请参阅子代类的文档,例如 HttpWebRequestFileWebRequest

适用于

另请参阅