HttpWebRequest.GetRequestStream 方法

定義

取得用來寫入要求資料的 Stream 物件。

多載

GetRequestStream()

取得用來寫入要求資料的 Stream 物件。

GetRequestStream(TransportContext)

取得 Stream 物件,此物件是用來寫入要求資料和輸出資料流相關聯的 TransportContext

GetRequestStream()

來源:
HttpWebRequest.cs
來源:
HttpWebRequest.cs
來源:
HttpWebRequest.cs

取得用來寫入要求資料的 Stream 物件。

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

例外狀況

Method 屬性是 GET 或 HEAD。

-或-

KeepAlivetrueAllowWriteStreamBufferingfalseContentLength 為 -1,SendChunkedfalse,且 Method 為 POST 或 PUT。

GetRequestStream() 方法已多次呼叫。

-或-

TransferEncoding 設定為值,且 SendChunkedfalse

要求快取驗證程式指出,可以從快取處理此要求的回應;不過,寫入資料的要求絕對不能使用快取。 如果您使用不正確實作的自訂快取驗證程式,便會發生這個例外狀況。

之前已呼叫過 Abort()

-或-

要求的逾時時限已過期。

-或-

處理這個要求時發生錯誤。

在 .NET Compact Framework 應用程式中,沒有正確取得並關閉內容長度為零的要求資料流。 如需處理零內容長度要求的詳細資訊,請參閱 .NET Compact Framework 中的網路程式設計

範例

下列程式碼範例會 GetRequestStream 使用 方法來傳回資料流程實例。

// 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 傳回資料流程,用來傳送 的資料 HttpWebRequestStream傳回 物件之後,您可以使用 方法來傳送資料 HttpWebRequestStream.Write

如果應用程式需要設定 屬性的值 ContentLength ,則必須先完成此動作,才能擷取資料流程。

您必須呼叫 Stream.Close 方法來關閉資料流程,並釋放連線以供重複使用。 無法關閉資料流程會導致您的應用程式用盡連線。

注意

您的應用程式無法混合特定要求的同步和非同步方法。 如果您呼叫 GetRequestStream 方法,則必須使用 GetResponse 方法來擷取回應。

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱.NET Framework中的網路追蹤

另請參閱

適用於

GetRequestStream(TransportContext)

來源:
HttpWebRequest.cs
來源:
HttpWebRequest.cs
來源:
HttpWebRequest.cs

取得 Stream 物件,此物件是用來寫入要求資料和輸出資料流相關聯的 TransportContext

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

例外狀況

GetRequestStream() 方法無法取得 Stream

GetRequestStream() 方法已多次呼叫。

-或-

TransferEncoding 設定為值,且 SendChunkedfalse

要求快取驗證程式指出,可以從快取處理此要求的回應;不過,寫入資料的要求絕對不能使用快取。 如果您使用不正確實作的自訂快取驗證程式,便會發生這個例外狀況。

Method 屬性是 GET 或 HEAD。

-或-

KeepAlivetrueAllowWriteStreamBufferingfalseContentLength 為 -1,SendChunkedfalse,且 Method 為 POST 或 PUT。

之前已呼叫過 Abort()

-或-

要求的逾時時限已過期。

-或-

處理這個要求時發生錯誤。

備註

方法 GetRequestStream 會傳回資料流程,用來傳送 資料流程的資料 HttpWebRequest ,並輸出 TransportContext 與資料流程相關聯的 。 Stream傳回 物件之後,您可以使用 方法來傳送資料 HttpWebRequestStream.Write

某些使用整合式Windows 驗證與擴充保護的應用程式,可能需要能夠查詢 所使用的 HttpWebRequest 傳輸層,以便從基礎 TLS 通道擷取通道系結權杖 (CBT) 。 方法 GetRequestStream 提供 HTTP 方法的存取權,這些方法具有要求主體 (POSTPUT 要求) 。 只有在應用程式實作自己的驗證,且需要存取 CBT 時,才需要此專案。

如果應用程式需要設定 屬性的值 ContentLength ,則必須先完成此動作,才能擷取資料流程。

您必須呼叫 Stream.Close 方法來關閉資料流程,並釋放連線以供重複使用。 無法關閉資料流程會導致您的應用程式用盡連線。

注意

您的應用程式無法混合特定要求的同步和非同步方法。 如果您呼叫 GetRequestStream 方法,則必須使用 GetResponse 方法來擷取回應。

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱.NET Framework中的網路追蹤

另請參閱

適用於