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 アプリケーションでコンテンツの長さが 0 の要求ストリームが取得されず、正しく閉じられませんでした。 コンテンツの長さが 0 の要求の処理の詳細については、「.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 認証を使用する一部のアプリケーションでは、基になる TLS チャネルからチャネル バインド トークン (CBT) を取得するために使用されるHttpWebRequestトランスポート層を照会できる必要がある場合があります。 このメソッドは GetRequestStream 、要求本文 (POST および要求) を持つ HTTP メソッドに対して PUT 、この情報へのアクセスを提供します。 これは、アプリケーションが独自の認証を実装していて、CBT へのアクセスが必要な場合にのみ必要です。

アプリケーションでプロパティの ContentLength 値を設定する必要がある場合は、ストリームを取得する前にこれを行う必要があります。

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

注意

アプリケーションでは、特定の要求に対して同期メソッドと非同期メソッドを混在させることはできません。 メソッドを呼び出す GetRequestStream 場合は、メソッドを GetResponse 使用して応答を取得する必要があります。

注意

このメンバーは、アプリケーションでネットワーク トレースが有効にされている場合にトレース情報を出力します。 詳細については、「.NET Frameworkのネットワーク トレース」を参照してください

こちらもご覧ください

適用対象