HttpWebRequest.GetRequestStream 方法

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

**命名空间:**System.Net
**程序集:**System(在 system.dll 中)

语法

声明
Public Overrides Function GetRequestStream As Stream
用法
Dim instance As HttpWebRequest
Dim returnValue As Stream

returnValue = instance.GetRequestStream
public override Stream GetRequestStream ()
public:
virtual Stream^ GetRequestStream () override
public Stream GetRequestStream ()
public override function GetRequestStream () : Stream

返回值

用来写入请求数据的 Stream

异常

异常类型 条件

ProtocolViolationException

Method 属性为 GET 或 HEAD。

- 或 -

KeepAlivetrueAllowWriteStreamBufferingfalseContentLength 为 -1,SendChunkedfalseMethod 为 POST 或 PUT。

InvalidOperationException

GetRequestStream 方法被调用多次。

- 或 -

TransferEncoding 被设置为一个值,并且 SendChunkedfalse

NotSupportedException

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

WebException

Abort 以前被调用过。

- 或 -

请求的超时期限到期。

- 或 -

处理请求时发生错误。

ObjectDisposedException

在 .NET Compact Framework 应用程序中,未正确获得和关闭一个内容长度为零的请求流。有关处理内容长度为零的请求的更多信息,请参见 .NET Compact Framework 中的网络编程

备注

GetRequestStream 方法返回用于发送 HttpWebRequest 数据的流。返回 Stream 对象后,可以通过使用 Stream.Write 方法发送带有 HttpWebRequest 的数据。

检索流之前必须先设置 ContentLength 属性的值。

必须调用 Stream.Close 方法关闭该流并释放连接以便重新使用。关闭该流失败将导致应用程序用尽连接。

提示

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

提示

当在应用程序中启用网络跟踪功能后,此成员将输出跟踪信息。有关更多信息,请参见 网络跟踪

示例

下面的代码示例使用 GetRequestStream 方法返回流实例。

' 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 (https://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()
// Set the 'Method' property of the 'Webrequest' to 'POST'.
myHttpWebRequest.Method = "POST";
Console.WriteLine ("\nPlease enter the data to be posted to the (https://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( "\nPlease enter the data to be posted to the (https://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.set_Method("POST");
Console.WriteLine("\nPlease enter the data to be posted to the "
    + "(https://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();
ubyte byte1[] = encoding.GetBytes(postData);

// Set the content type of the data being posted.
myHttpWebRequest.set_ContentType("application/x-www-form-"
    +"urlencoded");
// Set the content length of the string being posted.
myHttpWebRequest.set_ContentLength(postData.length());

Stream newStream = myHttpWebRequest.GetRequestStream();

newStream.Write(byte1, 0, byte1.length);
Console.WriteLine("The value of 'ContentLength' property after"
    + " sending the data is {0}", 
    System.Convert.ToString(myHttpWebRequest.get_ContentLength()));
// Close the Stream object.
newStream.Close();

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

HttpWebRequest 类
HttpWebRequest 成员
System.Net 命名空间

其他资源

defaultProxy 元素(网络设置)