HttpWebRequest.GetRequestStream メソッド
アセンブリ: System (system.dll 内)

Dim instance As HttpWebRequest Dim returnValue As Stream returnValue = instance.GetRequestStream
要求データを書き込むために使用する Stream。

例外の種類 | 条件 |
---|---|
ProtocolViolationException | Method プロパティが GET または HEAD です。 または KeepAlive は true で、AllowWriteStreamBuffering は false で、ContentLength は -1 で、SendChunked は false で、Method は POST または PUT です。 |
InvalidOperationException | GetRequestStream メソッドが、何度も呼び出されています。 または |
NotSupportedException | 要求のキャッシュ検証コントロールで、この要求の応答がキャッシュから取得できることが示されましたが、データの書き込みを行う要求でキャッシュは使用できません。この例外は、キャッシュ検証コントロールの不適切なカスタム実装を使用した場合に発生することがあります。 |
WebException | または または |
ObjectDisposedException | .NET Compact Framework アプリケーションで、コンテンツ長が 0 の要求ストリームは取得されず、適切に閉じられました。コンテンツ長が 0 の要求の処理の詳細については、「.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 (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()
// 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( "\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.set_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(); 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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- HttpWebRequest.GetRequestStream メソッドのページへのリンク