HttpWebRequest.GetRequestStream Metodo

Definizione

Ottiene un oggetto Stream da usare per scrivere i dati della richiesta.

Overload

GetRequestStream()

Ottiene un oggetto Stream da usare per scrivere i dati della richiesta.

GetRequestStream(TransportContext)

Ottiene un oggetto Stream da usare per scrivere i dati della richiesta e restituisce l'oggetto TransportContext associato al flusso.

GetRequestStream()

Origine:
HttpWebRequest.cs
Origine:
HttpWebRequest.cs
Origine:
HttpWebRequest.cs

Ottiene un oggetto Stream da usare per scrivere i dati della richiesta.

public:
 override System::IO::Stream ^ GetRequestStream();
public override System.IO.Stream GetRequestStream ();
override this.GetRequestStream : unit -> System.IO.Stream
Public Overrides Function GetRequestStream () As Stream

Restituisce

Oggetto Stream da usare per scrivere i dati della richiesta.

Eccezioni

La proprietà Method è GET o HEAD.

-oppure-

KeepAlive è true, AllowWriteStreamBuffering è false, ContentLength è -1, SendChunked è false e Method è POST o PUT.

Il metodo GetRequestStream() viene chiamato più volte.

-oppure-

TransferEncoding è impostato su un valore e SendChunked è false.

Il validator della cache delle richieste ha indicato che la risposta per questa richiesta può essere fornita dalla cache, ma le richieste che scrivono dati non usano la cache. Questa eccezione può verificarsi se si usa un validator della cache personalizzato che è implementato in modo non corretto.

Abort() è stato chiamato in precedenza.

-oppure-

Il periodo di timeout per la richiesta è scaduto.

-oppure-

Si è verificato un errore durante l'elaborazione della richiesta.

In un'applicazione .NET Compact Framework un flusso di richiesta di lunghezza del contenuto pari a zero non è stato ottenuto ed è stato chiuso correttamente. Per altre informazioni sulla gestione di richieste di lunghezza del contenuto pari a zero, vedere Programmazione di rete in .NET Compact Framework.

Esempio

Nell'esempio di codice seguente viene utilizzato il GetRequestStream metodo per restituire un'istanza del flusso.

// Set the 'Method' property of the 'Webrequest' to 'POST'.
myHttpWebRequest->Method = "POST";
Console::WriteLine( "\nPlease enter the data to be posted to the (http://www.contoso.com/codesnippets/next.asp) Uri :" );

// Create a new String* Object* to POST data to the Url.
String^ inputData = Console::ReadLine();

String^ postData = String::Concat( "firstone= ", inputData );
ASCIIEncoding^ encoding = gcnew ASCIIEncoding;
array<Byte>^ byte1 = encoding->GetBytes( postData );

// Set the content type of the data being posted.
myHttpWebRequest->ContentType = "application/x-www-form-urlencoded";

// Set the content length of the String* being posted.
myHttpWebRequest->ContentLength = byte1->Length;

Stream^ newStream = myHttpWebRequest->GetRequestStream();

newStream->Write( byte1, 0, byte1->Length );
Console::WriteLine( "The value of 'ContentLength' property after sending the data is {0}", myHttpWebRequest->ContentLength );

// Close the Stream Object*.
newStream->Close();
// Set the 'Method' property of the 'Webrequest' to 'POST'.
myHttpWebRequest.Method = "POST";
Console.WriteLine ("\nPlease enter the data to be posted to the (http://www.contoso.com/codesnippets/next.asp) Uri :");

// Create a new string object to POST data to the Url.
string inputData = Console.ReadLine ();


string postData = "firstone=" + inputData;
ASCIIEncoding encoding = new ASCIIEncoding ();
byte[] byte1 = encoding.GetBytes (postData);

// Set the content type of the data being posted.
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";

// Set the content length of the string being posted.
myHttpWebRequest.ContentLength = byte1.Length;

Stream newStream = myHttpWebRequest.GetRequestStream ();

newStream.Write (byte1, 0, byte1.Length);
Console.WriteLine ("The value of 'ContentLength' property after sending the data is {0}", myHttpWebRequest.ContentLength);

// Close the Stream object.
newStream.Close ();
' Set the 'Method' property of the 'Webrequest' to 'POST'.
myHttpWebRequest.Method = "POST"

Console.WriteLine(ControlChars.Cr + "Please enter the data to be posted to the (http://www.contoso.com/codesnippets/next.asp) Uri :")
' Create a new string object to POST data to the Url.
Dim inputData As String = Console.ReadLine()
Dim postData As String = "firstone" + ChrW(61) + inputData
Dim encoding As New ASCIIEncoding()
Dim byte1 As Byte() = encoding.GetBytes(postData)
' Set the content type of the data being posted.
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"
' Set the content length of the string being posted.
myHttpWebRequest.ContentLength = byte1.Length
Dim newStream As Stream = myHttpWebRequest.GetRequestStream()
newStream.Write(byte1, 0, byte1.Length)
Console.WriteLine("The value of 'ContentLength' property after sending the data is {0}", myHttpWebRequest.ContentLength)
newStream.Close()

Commenti

Il GetRequestStream metodo restituisce un flusso da usare per inviare dati per .HttpWebRequest Dopo che l'oggetto Stream è stato restituito, è possibile inviare dati con utilizzando HttpWebRequest il Stream.Write metodo .

Se un'applicazione deve impostare il valore della ContentLength proprietà, è necessario eseguire questa operazione prima di recuperare il flusso.

È necessario chiamare il Stream.Close metodo per chiudere il flusso e rilasciare la connessione per il riutilizzo. Se non si chiude il flusso, l'applicazione esaurisce le connessioni.

Nota

L'applicazione non può combinare metodi sincroni e asincroni per una determinata richiesta. Se si chiama il GetRequestStream metodo , è necessario usare il GetResponse metodo per recuperare la risposta.

Nota

Questo membro genera informazioni di traccia quando viene abilitata la funzionalità di traccia di rete nell'applicazione in uso. Per altre informazioni, vedere Traccia di rete in .NET Framework.

Vedi anche

Si applica a

GetRequestStream(TransportContext)

Origine:
HttpWebRequest.cs
Origine:
HttpWebRequest.cs
Origine:
HttpWebRequest.cs

Ottiene un oggetto Stream da usare per scrivere i dati della richiesta e restituisce l'oggetto TransportContext associato al flusso.

public:
 System::IO::Stream ^ GetRequestStream([Runtime::InteropServices::Out] System::Net::TransportContext ^ % context);
public System.IO.Stream GetRequestStream (out System.Net.TransportContext? context);
public System.IO.Stream GetRequestStream (out System.Net.TransportContext context);
override this.GetRequestStream : TransportContext -> System.IO.Stream
Public Function GetRequestStream (ByRef context As TransportContext) As Stream

Parametri

context
TransportContext

Oggetto TransportContext per l'oggetto Stream.

Restituisce

Oggetto Stream da usare per scrivere i dati della richiesta.

Eccezioni

Il metodo GetRequestStream() non è riuscito a ottenere l'oggetto Stream.

Il metodo GetRequestStream() viene chiamato più volte.

-oppure-

TransferEncoding è impostato su un valore e SendChunked è false.

Il validator della cache delle richieste ha indicato che la risposta per questa richiesta può essere fornita dalla cache, ma le richieste che scrivono dati non usano la cache. Questa eccezione può verificarsi se si usa un validator della cache personalizzato che è implementato in modo non corretto.

La proprietà Method è GET o HEAD.

-oppure-

KeepAlive è true, AllowWriteStreamBuffering è false, ContentLength è -1, SendChunked è false e Method è POST o PUT.

Abort() è stato chiamato in precedenza.

-oppure-

Il periodo di timeout per la richiesta è scaduto.

-oppure-

Si è verificato un errore durante l'elaborazione della richiesta.

Commenti

Il GetRequestStream metodo restituisce un flusso da usare per inviare dati per e HttpWebRequest restituisce l'oggetto TransportContext associato al flusso. Dopo che l'oggetto Stream è stato restituito, è possibile inviare dati con utilizzando HttpWebRequest il Stream.Write metodo .

Alcune applicazioni che usano autenticazione di Windows integrate con protezione estesa potrebbero dover eseguire query sul livello di trasporto usato da HttpWebRequest per recuperare il token di associazione del canale (CBT) dal canale TLS sottostante. Il GetRequestStream metodo fornisce l'accesso a queste informazioni per i metodi HTTP che hanno un corpo della richiesta (POST e PUT le richieste). Questa operazione è necessaria solo se l'applicazione implementa la propria autenticazione e deve accedere al cbt.

Se un'applicazione deve impostare il valore della ContentLength proprietà, è necessario eseguire questa operazione prima di recuperare il flusso.

È necessario chiamare il Stream.Close metodo per chiudere il flusso e rilasciare la connessione per il riutilizzo. Se non si chiude il flusso, l'applicazione esaurisce le connessioni.

Nota

L'applicazione non può combinare metodi sincroni e asincroni per una determinata richiesta. Se si chiama il GetRequestStream metodo , è necessario usare il GetResponse metodo per recuperare la risposta.

Nota

Questo membro genera informazioni di traccia quando viene abilitata la funzionalità di traccia di rete nell'applicazione in uso. Per altre informazioni, vedere Traccia di rete in .NET Framework.

Vedi anche

Si applica a