WebRequest.ContentLength 属性

定义

在后代类中重写时,获取或设置要发送的请求数据的内容长度。

public virtual long ContentLength { get; set; }

属性值

要发送的请求数据的字节数。

例外

如果未在子代类中重写属性,则会尝试获取或设置该属性。

示例

以下示例将 ContentLength 属性设置为传出字节缓冲区中的字节量。


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

注解

注意

WebRequestHttpWebRequestServicePointWebClient 已过时,不应将其用于新开发。 请改用 HttpClient

ContentLength 属性包含 WebRequest 实例发送到 Internet 资源的数据字节数。

备注

WebRequest 类是 abstract 类。 运行时 WebRequest 实例的实际行为由 WebRequest.Create 方法返回的后代类确定。 有关默认值和异常的详细信息,请参阅子代类的文档,例如 HttpWebRequestFileWebRequest

适用于

产品 版本
.NET 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

另请参阅