HttpWebRequest.GetRequestStream 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得 Stream 一個物件來寫入請求資料。
多載
| 名稱 | Description |
|---|---|
| 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 A 用來寫入請求資料。
例外狀況
該 Method 物業為 GET 或 HEAD。
-或-
KeepAlive是true,AllowWriteStreamBuffering是,ContentLength是false-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 = "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()
備註
謹慎
WebRequest、、HttpWebRequest、ServicePointWebClient、和 都已過時,你不應該用它們來做新開發。 請改用 HttpClient。
該方法會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
該 TransportContext 為 Stream。
傳回
Stream A 用來寫入請求資料。
例外狀況
該 GetRequestStream() 方法無法取得 Stream。
要求快取驗證程式表示可從快取提供此要求的回應;不過,寫入數據的要求不得使用快取。 如果您使用未正確實作的自定義快取驗證程式,就可能發生此例外狀況。
該 Method 物業為 GET 或 HEAD。
-或-
KeepAlive是true,AllowWriteStreamBuffering是,ContentLength是false-1,SendChunked是false,且Method是POST或PUT。
備註
謹慎
WebRequest、、HttpWebRequest、ServicePointWebClient、和 都已過時,你不應該用它們來做新開發。 請改用 HttpClient。
該方法回 GetRequestStream 傳一個串流,用於傳送資料, HttpWebRequest 並輸出與串流相關的資料 TransportContext 。 物件回傳後Stream,你可以用 的方法Stream.Write傳送資料HttpWebRequest。
某些使用整合 Windows 認證並擴展保護的應用程式,可能需要查詢所 HttpWebRequest 使用的傳輸層,以從底層 TLS 通道取得通道綁定令牌(CBT)。 此 GetRequestStream 方法為具有請求主體(POST 及 PUT 請求)的 HTTP 方法提供存取這些資訊。 只有在應用程式實作自己的驗證,且需要存取 CBT 時,才需要此專案。
若應用程式需設定屬性值 ContentLength ,則必須在取得串流前完成此操作。
你必須呼叫 Stream.Close 方法關閉串流並釋放連線以便重複使用。 無法關閉資料流會導致應用程式用盡連線。
注意
您的應用程式無法混合特定要求的同步和異步方法。 如果你呼叫該 GetRequestStream 方法,必須使用該 GetResponse 方法來取得回應。
注意
當您在應用程式中啟用網路追蹤時,此成員會輸出追蹤資訊。 欲了解更多資訊,請參閱 .NET Framework 中的網路追蹤。
另請參閱
- TransportContext
- GetChannelBinding(ChannelBindingKind)
- System.Security.Authentication.ExtendedProtection
- ChannelBinding
- 具有擴充保護的整合式 Windows 驗證