HttpWebRequest.GetRequestStream 方法

定义

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

重载

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

用于写入请求数据的 Stream

例外

Method 属性为 GET 或 HEAD。

GetRequestStream() 方法被调用多次。

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

之前已调用 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 返回用于发送 HttpWebRequest数据的流。 Stream返回对象后,可以使用该方法发送数据HttpWebRequestStream.Write

如果应用程序需要设置属性的值 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

参数

返回

Stream

用于写入请求数据的 Stream

例外

GetRequestStream() 方法无法获取 Stream

GetRequestStream() 方法被调用多次。

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

Method 属性为 GET 或 HEAD。

之前已调用 Abort()

  • 或 - 请求的超时期限到期。

  • 或 - 处理该请求时出错。

注解

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

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

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

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

备注

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

备注

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

另请参阅

适用于