次の方法で共有


HttpWebRequest.GetRequestStream メソッド

要求データを書き込むために使用する Stream インスタンスを取得します。

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 です。

または

KeepAlivetrue で、 AllowWriteStreamBufferingfalse で、 ContentLength は -1 で、 SendChunkedfalse で、 Method は POST または PUT です。

InvalidOperationException GetRequestStream メソッドが、何度も呼び出されています。

または

TransferEncoding に値が設定され、 SendChunkedfalse です。

WebException Abort は既に呼び出されました。

または

要求のタイムアウト時間が経過しました。

または

要求の処理中にエラーが発生しました。

解説

GetRequestStream メソッドは、 HttpWebRequest のデータを送信するために使用するストリームを返します。 Stream インスタンスが返されると、 Stream.Write メソッドを使用して、 HttpWebRequest でデータを送信できます。

メモ   ストリームを取得する前に、 ContentLength プロパティの値を設定する必要があります。

注意   ストリームを閉じて、再使用のための接続を解放するために Stream.Close メソッドを呼び出す必要があります。ストリームを閉じないと、アプリケーションで接続が不足することがあります。

使用例

[Visual Basic, C#, C++] GetRequestStream メソッドを使用して、ストリーム インスタンスを返す例を次に示します。

 
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 = 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 Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

HttpWebRequest クラス | HttpWebRequest メンバ | System.Net 名前空間