HttpWebRequest.GetRequestStream メソッド

定義

要求データを書き込むために使用する Stream オブジェクトを取得します。

オーバーロード

GetRequestStream()

要求データを書き込むために使用する Stream オブジェクトを取得します。

GetRequestStream(TransportContext)

要求データを書き込むために使用する Stream オブジェクトを取得し、そのストリームに関連付けられている TransportContext を出力します。

GetRequestStream()

ソース:
HttpWebRequest.cs
ソース:
HttpWebRequest.cs
ソース:
HttpWebRequest.cs

要求データを書き込むために使用する 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

例外

Method プロパティは GET または HEAD です。

- または -

KeepAlivetrueAllowWriteStreamBufferingfalseContentLength が -1、SendChunkedfalse で、Method が POST か PUT です。

GetRequestStream() メソッドが複数回呼び出されています。

- または -

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

要求キャッシュの検証コントロールは、この要求に対する応答がキャッシュから提供されることを示していますが、データを書き込む要求ではキャッシュを使用してはなりません。 正しく実装されていないカスタム キャッシュの検証コントロールを使用すると、この例外が発生する場合があります。

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 返されたら、 メソッドを使用して を HttpWebRequest 使用してデータを Stream.Write 送信できます。

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

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

注意

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

Note

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

こちらもご覧ください

適用対象

GetRequestStream(TransportContext)

ソース:
HttpWebRequest.cs
ソース:
HttpWebRequest.cs
ソース:
HttpWebRequest.cs

要求データを書き込むために使用する 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

例外

GetRequestStream() メソッドが Stream を取得できませんでした。

GetRequestStream() メソッドが複数回呼び出されています。

- または -

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

要求キャッシュの検証コントロールは、この要求に対する応答がキャッシュから提供されることを示していますが、データを書き込む要求ではキャッシュを使用してはなりません。 正しく実装されていないカスタム キャッシュの検証コントロールを使用すると、この例外が発生する場合があります。

Method プロパティは GET または HEAD です。

- または -

KeepAlivetrueAllowWriteStreamBufferingfalseContentLength が -1、SendChunkedfalse で、Method が POST か PUT です。

Abort() は以前に呼び出されました。

- または -

要求のタイムアウト期間の期限が切れました。

- または -

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

注釈

メソッドは GetRequestStream 、 のデータ HttpWebRequest を送信するために使用するストリームを返し、ストリームに関連付けられている を TransportContext 出力します。 オブジェクトが Stream 返されたら、 メソッドを使用して を HttpWebRequest 使用してデータを Stream.Write 送信できます。

拡張保護で統合Windows 認証を使用する一部のアプリケーションでは、基になる TLS チャネルからチャネル バインド トークン (CBT) を取得するために、 によってHttpWebRequest使用されるトランスポート層に対してクエリを実行できる必要がある場合があります。 メソッドは GetRequestStream 、要求本文 (POST と 要求) を持つ HTTP メソッドに対して PUT 、この情報へのアクセスを提供します。 これは、アプリケーションが独自の認証を実装していて、CBT へのアクセスが必要な場合にのみ必要です。

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

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

注意

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

Note

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

こちらもご覧ください

適用対象