WebRequest.GetRequestStream 方法

定义

当在子类中重写时,返回用于将数据写入 Internet 资源的 Stream

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

用于将数据写入 Internet 资源的 Stream

例外

当未在子类中重写该方法时,试图访问该方法。

示例

以下示例使用该方法 GetRequestStream 获取流,然后写入该流的数据。

// 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 启动向 Internet 资源发送数据的请求,并返回用于 Stream 将数据发送到 Internet 资源的实例。

该方法 GetRequestStream 提供对 . 的 Stream同步访问。 对于异步访问,请使用 BeginGetRequestStreamEndGetRequestStream 方法。

备注

WebRequest 类是一个 abstract 类。 运行时实例的实际行为由方法返回WebRequest.CreateWebRequest后代类确定。 有关默认值和异常的详细信息,请参阅子代类的文档,例如 HttpWebRequestFileWebRequest

适用于

另请参阅