WebRequest.GetRequestStream 方法

定義

在子代類別中覆寫時,傳回 Stream,以便將資料寫入至網際網路資源。When overridden in a descendant class, returns a Stream for writing data to the Internet resource.

public:
 virtual System::IO::Stream ^ GetRequestStream();
public virtual System.IO.Stream GetRequestStream ();
abstract member GetRequestStream : unit -> System.IO.Stream
override this.GetRequestStream : unit -> System.IO.Stream
Public Overridable Function GetRequestStream () As Stream

傳回

Stream

Stream,用於將資料寫入網際網路資源。A Stream for writing data to the Internet resource.

例外狀況

當方法在子代類別中未覆寫時,會嘗試存取該方法。Any attempt is made to access the method, when the method is not overridden in a descendant class.

範例

下列範例會使用 GetRequestStream 方法來取得資料流程,然後寫入資料流程的資料流程。The following example uses the GetRequestStream method to obtain a stream and then writes data that stream.

// Set the 'ContentType' property of the WebRequest.
myWebRequest->ContentType = "application/x-www-form-urlencoded";

// Set the 'ContentLength' property of the WebRequest.
myWebRequest->ContentLength = byteArray->Length;
Stream^ newStream = myWebRequest->GetRequestStream();
newStream->Write( byteArray, 0, byteArray->Length );

// Close the Stream object.
newStream->Close();

// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse^ myWebResponse = myWebRequest->GetResponse();

// Set the 'ContentType' property of the WebRequest.
myWebRequest.ContentType="application/x-www-form-urlencoded";

// Set the 'ContentLength' property of the WebRequest.
myWebRequest.ContentLength=byteArray.Length;
Stream newStream=myWebRequest.GetRequestStream();
newStream.Write(byteArray,0,byteArray.Length);

// Close the Stream object.
newStream.Close();

// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse=myWebRequest.GetResponse();



' Set the 'ContentType' property of the WebRequest.
myWebRequest.ContentType = "application/x-www-form-urlencoded"

' Set the 'ContentLength' property of the WebRequest.
myWebRequest.ContentLength = byteArray.Length
Dim newStream As Stream = myWebRequest.GetRequestStream()
newStream.Write(byteArray, 0, byteArray.Length)

' Close the Stream object.
newStream.Close()

' Assign the response object of 'WebRequest' to a 'WebResponse' variable.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

備註

GetRequestStream方法會起始將資料傳送至網際網路資源的要求,並傳回將 Stream 資料傳送至網際網路資源的實例。The GetRequestStream method initiates a request to send data to the Internet resource and returns a Stream instance for sending data to the Internet resource.

GetRequestStream方法會提供的同步存取 StreamThe GetRequestStream method provides synchronous access to the Stream. 針對非同步存取,請使用 BeginGetRequestStreamEndGetRequestStream 方法。For asynchronous access, use the BeginGetRequestStream and EndGetRequestStream methods.

注意

WebRequest類別是 abstract 類別。The WebRequest class is an abstract class. WebRequest實例在執行時間的實際行為是由方法所傳回的子代類別所決定 WebRequest.CreateThe actual behavior of WebRequest instances at run time is determined by the descendant class returned by the WebRequest.Create method. 如需預設值和例外狀況的詳細資訊,請參閱下階類別的檔,例如 HttpWebRequestFileWebRequestFor more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.

適用於

另請參閱