HttpWebRequest.GetRequestStream 方法

定义

获取用于写入请求数据的 Stream 对象。

重载

GetRequestStream()

获取用于写入请求数据的 Stream 对象。

GetRequestStream(TransportContext)

获取用于写入请求数据的 Stream 对象,并输出与流关联的 TransportContext

GetRequestStream()

Source:
HttpWebRequest.cs
Source:
HttpWebRequest.cs
Source:
HttpWebRequest.cs

获取用于写入请求数据的 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。

- 或 -

KeepAlivetrueAllowWriteStreamBufferingfalseContentLength 为 -1,SendChunkedfalseMethod 为 POST 或 PUT。

GetRequestStream() 方法被调用多次。

- 或 -

TransferEncoding 设置为一个值,且 SendChunkedfalse

请求缓存验证程序指示对此请求的响应可从缓存中提供;但是,写入数据的请求不得使用缓存。 如果你正在使用错误实现的自定义缓存验证程序,则会发生此异常。

之前已调用 Abort()

- 或 -

请求的超时期限到期。

- 或 -

处理该请求时出错。

在 .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 返回一个流,用于发送 的数据 HttpWebRequestStream返回 对象后,可以使用 方法通过 HttpWebRequestStream.Write 发送数据。

如果应用程序需要设置 属性的值 ContentLength ,则必须在检索流之前完成此操作。

必须调用 Stream.Close 方法来关闭流并释放连接以供重复使用。 未能关闭流会导致应用程序连接不足。

注意

应用程序不能对特定请求混合使用同步和异步方法。 如果调用 GetRequestStream 方法,则必须使用 GetResponse 方法来检索响应。

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅.NET Framework中的网络跟踪

另请参阅

适用于

GetRequestStream(TransportContext)

Source:
HttpWebRequest.cs
Source:
HttpWebRequest.cs
Source:
HttpWebRequest.cs

获取用于写入请求数据的 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

参数

返回

用于写入请求数据的 Stream

例外

GetRequestStream() 方法无法获取 Stream

GetRequestStream() 方法被调用多次。

- 或 -

TransferEncoding 设置为一个值,且 SendChunkedfalse

请求缓存验证程序指示对此请求的响应可从缓存中提供;但是,写入数据的请求不得使用缓存。 如果你正在使用错误实现的自定义缓存验证程序,则会发生此异常。

Method 属性为 GET 或 HEAD。

- 或 -

KeepAlivetrueAllowWriteStreamBufferingfalseContentLength 为 -1,SendChunkedfalseMethod 为 POST 或 PUT。

之前已调用 Abort()

- 或 -

请求的超时期限到期。

- 或 -

处理该请求时出错。

注解

方法 GetRequestStream 返回一个流,用于发送 的数据 HttpWebRequest ,并输出 TransportContext 与流关联的 。 Stream返回 对象后,可以使用 方法通过 HttpWebRequestStream.Write 发送数据。

某些使用具有扩展保护的集成Windows 身份验证的应用程序可能需要能够查询 使用的HttpWebRequest传输层,以便从基础 TLS 通道检索通道绑定令牌 (CBT) 。 方法 GetRequestStream 为 HTTP 方法提供对此信息的访问,这些方法具有请求正文 (POSTPUT 请求) 。 仅当应用程序实现自己的身份验证并需要访问 CBT 时,才需要此项。

如果应用程序需要设置 属性的值 ContentLength ,则必须在检索流之前完成此操作。

必须调用 Stream.Close 方法来关闭流并释放连接以供重复使用。 未能关闭流会导致应用程序连接不足。

注意

应用程序不能对特定请求混合使用同步和异步方法。 如果调用 GetRequestStream 方法,则必须使用 GetResponse 方法来检索响应。

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅.NET Framework中的网络跟踪

另请参阅

适用于