HttpWebRequest.GetRequestStream 方法

定义

获取用于写入请求数据的 Stream 对象。Gets a Stream object to use to write request data.

重载

GetRequestStream()

获取用于写入请求数据的 Stream 对象。Gets a Stream object to use to write request data.

GetRequestStream(TransportContext)

获取用于写入请求数据的 Stream 对象,并输出与该流关联的 TransportContextGets a Stream object to use to write request data and outputs the TransportContext associated with the stream.

GetRequestStream()

获取用于写入请求数据的 Stream 对象。Gets a Stream object to use to write request data.

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

返回

用于写入请求数据的 StreamA Stream to use to write request data.

异常

Method 属性为 GET 或 HEAD。The Method property is GET or HEAD.

-or- KeepAlivetrueAllowWriteStreamBufferingfalseContentLength 为 -1,SendChunkedfalseMethod 为 POST 或 PUT。KeepAlive is true, AllowWriteStreamBuffering is false, ContentLength is -1, SendChunked is false, and Method is POST or PUT.

GetRequestStream() 方法被调用多次。The GetRequestStream() method is called more than once.

-or- TransferEncoding 设置为一个值,且 SendChunkedfalseTransferEncoding is set to a value and SendChunked is false.

请求缓存验证程序指示对此请求的响应可从缓存中提供;但是,写入数据的请求不得使用缓存。The request cache validator indicated that the response for this request can be served from the cache; however, requests that write data must not use the cache. 如果你正在使用错误实现的自定义缓存验证程序,则会发生此异常。This exception can occur if you are using a custom cache validator that is incorrectly implemented.

之前已调用 Abort()Abort() was previously called.

-or- 请求的超时期限到期。The time-out period for the request expired.

-or- 处理请求时出错。An error occurred while processing the request.

在 .NET Compact Framework 应用程序中,未正确获得和关闭一个内容长度为零的请求流。In a .NET Compact Framework application, a request stream with zero content length was not obtained and closed correctly. 有关处理内容长度为零的请求的详细信息,请参阅 .NET Compact Framework 中的网络编程For more information about handling zero content length requests, see Network Programming in the .NET Compact Framework.

示例

下面的代码示例使用 GetRequestStream 方法返回流实例。The following code example uses the GetRequestStream method to return a stream instance.

// 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的数据的流。The GetRequestStream method returns a stream to use to send data for the HttpWebRequest. 返回 Stream 对象之后,你可以使用 Stream.Write 方法通过 HttpWebRequest 发送数据。After the Stream object has been returned, you can send data with the HttpWebRequest by using the Stream.Write method.

如果应用程序需要设置 ContentLength 属性的值,则必须在检索流前完成此操作。If an application needs to set the value of the ContentLength property, then this must be done before retrieving the stream.

必须调用 Stream.Close 方法来关闭流并释放连接以供重用。You must call the Stream.Close method to close the stream and release the connection for reuse. 未能关闭流将导致应用程序无法连接。Failure to close the stream causes your application to run out of connections.

备注

应用程序不能为特定请求混合使用同步和异步方法。Your application cannot mix synchronous and asynchronous methods for a particular request. 如果调用 GetRequestStream 方法,则必须使用 GetResponse 方法来检索响应。If you call the GetRequestStream method, you must use the GetResponse method to retrieve the response.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

另请参阅

GetRequestStream(TransportContext)

获取用于写入请求数据的 Stream 对象,并输出与该流关联的 TransportContextGets a Stream object to use to write request data and outputs the TransportContext associated with the stream.

public:
 System::IO::Stream ^ GetRequestStream([Runtime::InteropServices::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

参数

返回

用于写入请求数据的 StreamA Stream to use to write request data.

异常

GetRequestStream() 方法无法获取 StreamThe GetRequestStream() method was unable to obtain the Stream.

GetRequestStream() 方法被调用多次。The GetRequestStream() method is called more than once.

-or- TransferEncoding 设置为一个值,且 SendChunkedfalseTransferEncoding is set to a value and SendChunked is false.

请求缓存验证程序指示对此请求的响应可从缓存中提供;但是,写入数据的请求不得使用缓存。The request cache validator indicated that the response for this request can be served from the cache; however, requests that write data must not use the cache. 如果你正在使用错误实现的自定义缓存验证程序,则会发生此异常。This exception can occur if you are using a custom cache validator that is incorrectly implemented.

Method 属性为 GET 或 HEAD。The Method property is GET or HEAD.

-or- KeepAlivetrueAllowWriteStreamBufferingfalseContentLength 为 -1,SendChunkedfalseMethod 为 POST 或 PUT。KeepAlive is true, AllowWriteStreamBuffering is false, ContentLength is -1, SendChunked is false, and Method is POST or PUT.

之前已调用 Abort()Abort() was previously called.

-or- 请求的超时期限到期。The time-out period for the request expired.

-or- 处理请求时出错。An error occurred while processing the request.

注解

GetRequestStream 方法返回一个用于发送 HttpWebRequest 数据的流,并输出与该流关联的 TransportContextThe GetRequestStream method returns a stream to use to send data for the HttpWebRequest and outputs the TransportContext associated with the stream. 返回 Stream 对象之后,你可以使用 Stream.Write 方法通过 HttpWebRequest 发送数据。After the Stream object has been returned, you can send data with the HttpWebRequest by using the Stream.Write method.

某些使用具有扩展保护的集成 Windows 身份验证的应用程序可能需要能够查询 HttpWebRequest 使用的传输层,以便从基础 TLS 通道检索通道绑定令牌(CBT)。Some applications that use integrated Windows authentication with extended protection may need to be able to query the transport layer used by HttpWebRequest in order to retrieve the channel binding token (CBT) from the underlying TLS channel. GetRequestStream 方法为具有请求正文的 HTTP 方法(POSTPUT 请求)提供对此信息的访问。The GetRequestStream method provides access to this information for HTTP methods which have a request body (POST and PUT requests). 仅当应用程序要实现其自己的身份验证并需要访问 CBT 时,才需要此权限。This is only needed if the application is implementing its own authentication and needs access to the CBT.

如果应用程序需要设置 ContentLength 属性的值,则必须在检索流前完成此操作。If an application needs to set the value of the ContentLength property, then this must be done before retrieving the stream.

必须调用 Stream.Close 方法来关闭流并释放连接以供重用。You must call the Stream.Close method to close the stream and release the connection for reuse. 未能关闭流将导致应用程序无法连接。Failure to close the stream causes your application to run out of connections.

备注

应用程序不能为特定请求混合使用同步和异步方法。Your application cannot mix synchronous and asynchronous methods for a particular request. 如果调用 GetRequestStream 方法,则必须使用 GetResponse 方法来检索响应。If you call the GetRequestStream method, you must use the GetResponse method to retrieve the response.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

另请参阅

适用于