HttpWebRequest.AllowWriteStreamBuffering Proprietà

Definizione

Ottiene o imposta un valore che indica se memorizzare nel buffer i dati inviati alla risorsa Internet.

C#
public virtual bool AllowWriteStreamBuffering { get; set; }
C#
public bool AllowWriteStreamBuffering { get; set; }

Valore della proprietà

true per abilitare il buffer dei dati inviati alla risorsa Internet; false per disabilitare il buffering. Il valore predefinito è true.

Esempio

Nell'esempio di codice seguente viene utilizzata la proprietà AllowWriteStreamBuffering per disabilitare il buffer dei dati.

C#

// Create a new 'HttpWebRequest' object to the mentioned Uri.				
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.contoso.com/codesnippets/next.asp");
// Set AllowWriteStreamBuffering to 'false'.
myHttpWebRequest.AllowWriteStreamBuffering=false;
Console.WriteLine("\nPlease Enter the data to be posted to the (http://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();

Commenti

Attenzione

WebRequest, HttpWebRequest, ServicePointe WebClient sono obsoleti e non è consigliabile usarli per nuovi sviluppi. Usare invece HttpClient.

Quando AllowWriteStreamBuffering è true, i dati vengono memorizzati nel buffer in memoria in modo che siano pronti per essere reinviati in caso di reindirizzamenti o richieste di autenticazione.

Note per gli eredi

L'impostazione di AllowWriteStreamBuffering su true potrebbe causare problemi di prestazioni durante il caricamento di set di dati di grandi dimensioni perché il buffer di dati potrebbe usare tutta la memoria disponibile.

Si applica a

Prodotto Versioni
.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