HttpWebRequest.GetRequestStream 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得用來寫入要求資料的 Stream 物件。
多載
GetRequestStream() |
取得用來寫入要求資料的 Stream 物件。 |
GetRequestStream(TransportContext) |
取得 Stream 物件,此物件是用來寫入要求資料和輸出資料流相關聯的 TransportContext。 |
GetRequestStream()
取得用來寫入要求資料的 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。
-或-
KeepAlive 是 true
,AllowWriteStreamBuffering 是 false
,ContentLength 為 -1,SendChunked 是 false
,且 Method 為 POST 或 PUT。
要求快取驗證程式指出,可以從快取處理此要求的回應;不過,寫入資料的要求絕對不能使用快取。 如果您使用不正確實作的自訂快取驗證程式,便會發生這個例外狀況。
在 .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 傳回數據流,用來傳送 的數據 HttpWebRequest。 Stream傳回 物件之後,您可以使用 方法來傳送Stream.Write數據HttpWebRequest。
如果應用程式需要設定 屬性的值 ContentLength ,則必須先完成此動作,才能擷取數據流。
您必須呼叫 Stream.Close 方法來關閉數據流,並釋放連線以供重複使用。 無法關閉資料流會導致您的應用程式用盡連線。
注意
您的應用程式無法混合特定要求的同步和異步方法。 如果您呼叫 GetRequestStream 方法,則必須使用 GetResponse 方法來擷取回應。
注意
在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤。
另請參閱
適用於
GetRequestStream(TransportContext)
取得 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
參數
- context
- TransportContext
傳回
用以寫入要求資料的 Stream
例外狀況
GetRequestStream() 方法無法取得 Stream。
要求快取驗證程式指出,可以從快取處理此要求的回應;不過,寫入資料的要求絕對不能使用快取。 如果您使用不正確實作的自訂快取驗證程式,便會發生這個例外狀況。
Method 屬性是 GET 或 HEAD。
-或-
KeepAlive 是 true
,AllowWriteStreamBuffering 是 false
,ContentLength 為 -1,SendChunked 是 false
,且 Method 為 POST 或 PUT。
備註
方法 GetRequestStream 會傳回資料流,用來傳送 資料流的數據 HttpWebRequest ,並輸出 TransportContext 與資料流相關聯的 。 Stream傳回 物件之後,您可以使用 方法來傳送Stream.Write數據HttpWebRequest。
某些使用整合式 Windows 驗證 與擴充保護的應用程式,可能需要能夠查詢 所使用的HttpWebRequest傳輸層,以便從基礎 TLS 通道擷取通道系結令牌 (CBT) 。 方法 GetRequestStream 提供 HTTP 方法的存取權,這些方法具有要求主體 (POST
和 PUT
要求) 。 只有在應用程式實作自己的驗證,且需要存取 CBT 時,才需要此專案。
如果應用程式需要設定 屬性的值 ContentLength ,則必須先完成此動作,才能擷取數據流。
您必須呼叫 Stream.Close 方法來關閉數據流,並釋放連線以供重複使用。 無法關閉資料流會導致您的應用程式用盡連線。
注意
您的應用程式無法混合特定要求的同步和異步方法。 如果您呼叫 GetRequestStream 方法,則必須使用 GetResponse 方法來擷取回應。
注意
在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤。
另請參閱
- TransportContext
- GetChannelBinding(ChannelBindingKind)
- System.Security.Authentication.ExtendedProtection
- ChannelBinding
- 具有擴充保護的整合式 Windows 驗證
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應