HttpWebRequest.GetRequestStream 方法

定義

取得用來寫入要求資料的 Stream 物件。Gets a Stream object to use to write request data.

多載

GetRequestStream()

取得用來寫入要求資料的 Stream 物件。Gets a Stream object to use to write request data.

GetRequestStream(TransportContext)

取得 Stream 物件,此物件是用來寫入要求資料和輸出資料流相關聯的 TransportContextGets a Stream object to use to write request data and outputs the TransportContext associated with the stream.

GetRequestStream()

取得用來寫入要求資料的 Stream 物件。Gets a Stream object to use to write request data.

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

傳回

Stream

用以寫入要求資料的 StreamA Stream to use to write request data.

例外狀況

Method 屬性是 GET 或 HEAD。The Method property is GET or HEAD.

-或--or- KeepAlivetrueAllowWriteStreamBufferingfalseContentLength 為 -1,SendChunkedfalse,且 Method 為 POST 或 PUT。KeepAlive is true, AllowWriteStreamBuffering is false, ContentLength is -1, SendChunked is false, and Method is POST or PUT.

GetRequestStream() 方法已多次呼叫。The GetRequestStream() method is called more than once.

-或--or- TransferEncoding 設定為值,且 SendChunkedfalseTransferEncoding is set to a value and SendChunked is false.

要求快取驗證程式指出,可以從快取處理此要求的回應;不過,寫入資料的要求絕對不能使用快取。The request cache validator indicated that the response for this request can be served from the cache; however, requests that write data must not use the cache. 如果您使用不正確實作的自訂快取驗證程式,便會發生這個例外狀況。This exception can occur if you are using a custom cache validator that is incorrectly implemented.

之前已呼叫過 Abort()Abort() was previously called.

-或--or- 要求的逾時時限已過期。The time-out period for the request expired.

-或--or- 處理這個要求時發生錯誤。An error occurred while processing the request.

在 .NET Compact Framework 應用程式中,沒有正確取得並關閉內容長度為零的要求資料流。In a .NET Compact Framework application, a request stream with zero content length was not obtained and closed correctly. 如需處理零內容長度要求的詳細資訊,請參閱 .NET Compact Framework 中的網路程式設計For more information about handling zero content length requests, see Network Programming in the .NET Compact Framework.

範例

下列程式碼範例會使用 GetRequestStream 方法來傳回資料流程實例。The following code example uses the GetRequestStream method to return a stream instance.

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

備註

方法會傳回 GetRequestStream 用來傳送資料的資料流程 HttpWebRequestThe GetRequestStream method returns a stream to use to send data for the HttpWebRequest. Stream傳回物件之後,您可以使用方法,透過來傳送資料 HttpWebRequest Stream.WriteAfter the Stream object has been returned, you can send data with the HttpWebRequest by using the Stream.Write method.

如果應用程式需要設定屬性的值 ContentLength ,則必須在抓取資料流程之前完成這項作業。If an application needs to set the value of the ContentLength property, then this must be done before retrieving the stream.

您必須呼叫 Stream.Close 方法來關閉資料流程,並釋放連接以供重複使用。You must call the Stream.Close method to close the stream and release the connection for reuse. 無法關閉資料流程,會導致您的應用程式用盡連接。Failure to close the stream causes your application to run out of connections.

注意

您的應用程式無法混合特定要求的同步和非同步方法。Your application cannot mix synchronous and asynchronous methods for a particular request. 如果您呼叫 GetRequestStream 方法,就必須使用 GetResponse 方法來取出回應。If you call the GetRequestStream method, you must use the GetResponse method to retrieve the response.

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。This member outputs trace information when you enable network tracing in your application. 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤For more information, see Network Tracing in the .NET Framework.

另請參閱

適用於

GetRequestStream(TransportContext)

取得 Stream 物件,此物件是用來寫入要求資料和輸出資料流相關聯的 TransportContextGets a Stream object to use to write request data and outputs the TransportContext associated with the stream.

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

參數

傳回

Stream

用以寫入要求資料的 StreamA Stream to use to write request data.

例外狀況

GetRequestStream() 方法無法取得 StreamThe GetRequestStream() method was unable to obtain the Stream.

GetRequestStream() 方法已多次呼叫。The GetRequestStream() method is called more than once.

-或--or- TransferEncoding 設定為值,且 SendChunkedfalseTransferEncoding is set to a value and SendChunked is false.

要求快取驗證程式指出,可以從快取處理此要求的回應;不過,寫入資料的要求絕對不能使用快取。The request cache validator indicated that the response for this request can be served from the cache; however, requests that write data must not use the cache. 如果您使用不正確實作的自訂快取驗證程式,便會發生這個例外狀況。This exception can occur if you are using a custom cache validator that is incorrectly implemented.

Method 屬性是 GET 或 HEAD。The Method property is GET or HEAD.

-或--or- KeepAlivetrueAllowWriteStreamBufferingfalseContentLength 為 -1,SendChunkedfalse,且 Method 為 POST 或 PUT。KeepAlive is true, AllowWriteStreamBuffering is false, ContentLength is -1, SendChunked is false, and Method is POST or PUT.

之前已呼叫過 Abort()Abort() was previously called.

-或--or- 要求的逾時時限已過期。The time-out period for the request expired.

-或--or- 處理這個要求時發生錯誤。An error occurred while processing the request.

備註

方法會傳回 GetRequestStream 資料流程,用來傳送的資料, HttpWebRequest 並輸出 TransportContext 與資料流程相關聯的。The GetRequestStream method returns a stream to use to send data for the HttpWebRequest and outputs the TransportContext associated with the stream. Stream傳回物件之後,您可以使用方法,透過來傳送資料 HttpWebRequest Stream.WriteAfter the Stream object has been returned, you can send data with the HttpWebRequest by using the Stream.Write method.

某些使用整合式 Windows 驗證搭配擴充保護的應用程式,可能需要能夠查詢所使用的傳輸層,才能 HttpWebRequest 從基礎 TLS 通道 (CBT) 抓取通道系結 token。Some applications that use integrated Windows authentication with extended protection may need to be able to query the transport layer used by HttpWebRequest in order to retrieve the channel binding token (CBT) from the underlying TLS channel. 方法可讓 GetRequestStream 具有要求本文 (POST 和要求) 的 HTTP 方法存取此資訊 PUTThe GetRequestStream method provides access to this information for HTTP methods which have a request body (POST and PUT requests). 只有當應用程式正在執行自己的驗證,而且需要存取 CBT 時,才需要進行此工作。This is only needed if the application is implementing its own authentication and needs access to the CBT.

如果應用程式需要設定屬性的值 ContentLength ,則必須在抓取資料流程之前完成這項作業。If an application needs to set the value of the ContentLength property, then this must be done before retrieving the stream.

您必須呼叫 Stream.Close 方法來關閉資料流程,並釋放連接以供重複使用。You must call the Stream.Close method to close the stream and release the connection for reuse. 無法關閉資料流程,會導致您的應用程式用盡連接。Failure to close the stream causes your application to run out of connections.

注意

您的應用程式無法混合特定要求的同步和非同步方法。Your application cannot mix synchronous and asynchronous methods for a particular request. 如果您呼叫 GetRequestStream 方法,就必須使用 GetResponse 方法來取出回應。If you call the GetRequestStream method, you must use the GetResponse method to retrieve the response.

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。This member outputs trace information when you enable network tracing in your application. 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤For more information, see Network Tracing in the .NET Framework.

另請參閱

適用於