HttpWebRequest.GetRequestStream 方法
获取用于写入请求数据的 Stream 实例。
[Visual Basic] Overrides Public Function GetRequestStream() As Stream [C#] public override Stream GetRequestStream(); [C++] public: Stream* GetRequestStream(); [JScript] public override function GetRequestStream() : Stream;
返回值
用来写入请求数据的 Stream。
异常
| 异常类型 | 条件 |
|---|---|
| ProtocolViolationException | Method 属性为 GET 或 HEAD。
- 或 - KeepAlive 为 true,AllowWriteStreamBuffering 为 false,ContentLength 为 -1,SendChunked 为 false,Method 为 POST 或 PUT。 |
| InvalidOperationException | GetRequestStream 方法被调用多次。
- 或 - TransferEncoding 被设置为一个值,并且 SendChunked 为 false。 |
| WebException | Abort 以前被调用过。
- 或 - 请求的超时期限到期。 - 或 - 处理请求时发生错误。 |
备注
GetRequestStream 方法返回用于发送 HttpWebRequest 数据的流。返回 Stream 实例后,可以通过使用 Stream.Write 方法发送带有 HttpWebRequest 的数据。
注意 检索流之前必须先设置 ContentLength 属性的值。
警告 必须调用 Stream.Close 方法关闭该流并释放连接以便重新使用。关闭该流失败将导致应用程序用尽连接。
示例
[Visual Basic, C#, C++] 下面的示例使用 GetRequestStream 方法返回流实例。
[Visual Basic] 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 = postData.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() [C#] 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=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}",myHttpWebRequest.ContentLength); // Close the Stream object. newStream.Close(); [C++] String* postData = String::Concat(S"firstone= ", inputData); ASCIIEncoding* encoding = new ASCIIEncoding(); Byte byte1[] = encoding->GetBytes(postData); // Set the content type of the data being posted. myHttpWebRequest->ContentType = S"application/x-www-form-urlencoded"; // Set the content length of the String* being posted. myHttpWebRequest->ContentLength=postData->Length; Stream* newStream = myHttpWebRequest->GetRequestStream(); newStream->Write(byte1, 0, byte1->Length); Console::WriteLine(S"The value of 'ContentLength' property after sending the data is {0}", __box(myHttpWebRequest->ContentLength)); // Close the Stream Object*. newStream->Close();
[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮
。
要求
平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版, 公共语言基础结构 (CLI) 标准
浙公网安备 33010602011771号