HttpWebRequest.AllowWriteStreamBuffering 属性

获取或设置一个值,该值指示是否对发送到 Internet 资源的数据进行缓冲处理。

**命名空间:**System.Net
**程序集:**System(在 system.dll 中)

语法

声明
Public Property AllowWriteStreamBuffering As Boolean
用法
Dim instance As HttpWebRequest
Dim value As Boolean

value = instance.AllowWriteStreamBuffering

instance.AllowWriteStreamBuffering = value
public bool AllowWriteStreamBuffering { get; set; }
public:
property bool AllowWriteStreamBuffering {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_AllowWriteStreamBuffering ()

/** @property */
public void set_AllowWriteStreamBuffering (boolean value)
public function get AllowWriteStreamBuffering () : boolean

public function set AllowWriteStreamBuffering (value : boolean)

属性值

true 允许对发送到 Internet 资源的数据进行缓冲处理,false 禁用缓冲处理。默认为 true

备注

AllowWriteStreamBufferingtrue 时,数据缓存在内存中,以便随时可以在重定向或身份验证请求时重新发送。

给实现者的说明AllowWriteStreamBuffering 设置为 true 可能会在上载大型数据集时造成性能问题,原因是数据缓冲区可能占用了所有的可用内存。

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE 平台说明: 出于性能考虑,此属性的默认值为 false;但是请注意,如果谓词需要实体数据(如 POST),就可能不会进行重定向及身份验证。要为 HTTP 请求实现完整的 .NET Framework 行为,请将 AllowWriteStreamBuffering 设置为 true

示例

下面的代码示例使用 AllowWriteStreamBuffering 属性来禁用数据缓冲。

 ' A new 'HttpWebRequest' object is created                 
 Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("https://www.contoso.com/codesnippets/next.asp"), HttpWebRequest)
' AllowWriteStreamBuffering is set to 'false' 
 myHttpWebRequest.AllowWriteStreamBuffering = False
 Console.WriteLine(ControlChars.Cr + "Please Enter the data to be posted to the (https://www.contoso.com/codesnippets/next.asp) uri:")
 Dim inputData As String = Console.ReadLine()
 Dim postData As String = "firstone" + ChrW(61) + inputData
 ' 'Method' property of 'HttpWebRequest' class is set to POST.
 myHttpWebRequest.Method = "POST"
 Dim encodedData As New ASCIIEncoding()
 Dim byteArray As Byte() = encodedData.GetBytes(postData)
 ' 'ContentType' property of the 'HttpWebRequest' class is set to "application/x-www-form-urlencoded".
 myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"
 ' If the AllowWriteStreamBuffering property of HttpWebRequest is set to false,then contentlength has to be set to length of data to be posted else Exception(411) Length required is raised.
  myHttpWebRequest.ContentLength=byteArray.Length
 Dim newStream As Stream = myHttpWebRequest.GetRequestStream()
 newStream.Write(byteArray, 0, byteArray.Length)
 newStream.Close()
 Console.WriteLine(ControlChars.Cr + "Data has been posted to the Uri" + ControlChars.Cr + ControlChars.Cr + "Please wait for the response..........")
 ' The response object of 'HttpWebRequest' is assigned to a 'HttpWebResponse' variable.
 Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
// Create a new 'HttpWebRequest' object to the mentioned Uri.                
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("https://www.contoso.com/codesnippets/next.asp");
// Set AllowWriteStreamBuffering to 'false'. 
myHttpWebRequest.AllowWriteStreamBuffering=false;
Console.WriteLine("\nPlease Enter the data to be posted to the (https://www.contoso.com/codesnippets/next.asp) uri:");
string inputData =Console.ReadLine();
string postData="firstone="+inputData;
// Set 'Method' property of 'HttpWebRequest' class to POST.
myHttpWebRequest.Method="POST";
ASCIIEncoding encodedData=new ASCIIEncoding();
byte[]  byteArray=encodedData.GetBytes(postData);
// Set 'ContentType' property of the 'HttpWebRequest' class to "application/x-www-form-urlencoded".
myHttpWebRequest.ContentType="application/x-www-form-urlencoded";
// If the AllowWriteStreamBuffering property of HttpWebRequest is set to false,the contentlength has to be set to length of data to be posted else Exception(411) is raised.
myHttpWebRequest.ContentLength=byteArray.Length;
Stream newStream=myHttpWebRequest.GetRequestStream();
newStream.Write(byteArray,0,byteArray.Length);
newStream.Close();
Console.WriteLine("\nData has been posted to the Uri\n\nPlease wait for the response..........");
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
// Create a new 'HttpWebRequest' object to the mentioned Uri.
HttpWebRequest^ myHttpWebRequest = (HttpWebRequest^)( WebRequest::Create( "https://www.contoso.com/codesnippets/next.asp" ) );
// Set AllowWriteStreamBuffering to 'false'.
myHttpWebRequest->AllowWriteStreamBuffering = false;
Console::WriteLine( "\nPlease Enter the data to be posted to the (https://www.contoso.com/codesnippets/next.asp) uri:" );
String^ inputData = Console::ReadLine();
String^ postData = String::Concat( "firstone= ", inputData );
// Set 'Method' property of 'HttpWebRequest' class to POST.
myHttpWebRequest->Method = "POST";
ASCIIEncoding^ encodedData = gcnew ASCIIEncoding;
array<Byte>^ byteArray = encodedData->GetBytes( postData );
// Set 'ContentType' property of the 'HttpWebRequest' class to S"application/x-www-form-urlencoded".
myHttpWebRequest->ContentType = "application/x-www-form-urlencoded";
// If the AllowWriteStreamBuffering property of HttpWebRequest is set to false, the contentlength has to be set to length of data to be posted else Exception(411) is raised.
myHttpWebRequest->ContentLength = byteArray->Length;
Stream^ newStream = myHttpWebRequest->GetRequestStream();
newStream->Write( byteArray, 0, byteArray->Length );
newStream->Close();
Console::WriteLine( "\nData has been posted to the Uri\n\nPlease wait for the response.........." );
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse^ myHttpWebResponse = (HttpWebResponse^)( myHttpWebRequest->GetResponse() );
// Create a new 'HttpWebRequest' object to the mentioned Uri.                
HttpWebRequest myHttpWebRequest = (HttpWebRequest)
    (WebRequest.Create("https://www.contoso.com/codesnippets/"
    + "next.asp"));
// Set AllowWriteStreamBuffering to 'false'. 
myHttpWebRequest.set_AllowWriteStreamBuffering(false);
Console.WriteLine("\nPlease Enter the data to be posted to the "
    + "(https://www.contoso.com/codesnippets/next.asp) uri:");
String inputData = Console.ReadLine();
String postData = "firstone=" + inputData;
// Set 'Method' property of 'HttpWebRequest' class to POST.
myHttpWebRequest.set_Method("POST");
ASCIIEncoding encodedData =  new ASCIIEncoding();
ubyte byteArray[] = encodedData.GetBytes(postData);
// Set 'ContentType' property of the 'HttpWebRequest' class to 
// "application/x-www-form-urlencoded".
myHttpWebRequest.set_ContentType
    ("application/x-www-form-urlencoded");
// If the AllowWriteStreamBuffering property of HttpWebRequest is
// set to false,the contentlength has to be set to length of data to
// be posted else Exception(411) is raised.
myHttpWebRequest.set_ContentLength(byteArray.length);
Stream newStream = myHttpWebRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.length);
newStream.Close();
Console.WriteLine("\nData has been posted to the Uri\n\nPlease "
    + "wait for the response..........");
// Assign the response object of 'HttpWebRequest' to a 
//'HttpWebResponse' variable.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)
    (myHttpWebRequest.GetResponse());

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

HttpWebRequest 类
HttpWebRequest 成员
System.Net 命名空间