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

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

Stream

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 usato il metodo per restituire un'istanza GetRequestStream di 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 aver restituito l'oggetto Stream , è possibile inviare dati con il HttpWebRequest metodo usando il Stream.Write metodo .

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

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

Nota

L'applicazione non può combinare metodi sincroni e asincroni per una richiesta specifica. Se si chiama il metodo, è necessario usare il GetRequestStream 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)

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

Stream

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 l'oggetto HttpWebRequest e restituisce l'oggetto TransportContext associato al flusso. Dopo aver restituito l'oggetto Stream , è possibile inviare dati con il HttpWebRequest metodo usando il Stream.Write metodo .

Alcune applicazioni che usano autenticazione di Windows integrate con protezione estesa possono essere in grado di eseguire query sul livello di trasporto usato da HttpWebRequest per recuperare il token di associazione del canale dal canale TLS sottostante. Il GetRequestStream metodo fornisce l'accesso a queste informazioni per i metodi HTTP che dispongono di un corpo della richiesta (POST e PUT 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 proprietà, è necessario eseguire questa operazione prima di ContentLength recuperare il flusso.

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

Nota

L'applicazione non può combinare metodi sincroni e asincroni per una richiesta specifica. Se si chiama il metodo, è necessario usare il GetRequestStream 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