次の方法で共有


WebRequest.EndGetRequestStream メソッド

派生クラスでオーバーライドされると、インターネット リソースにデータを書き込むための Stream を返します。

Public Overridable Function EndGetRequestStream( _
   ByVal asyncResult As IAsyncResult _) As Stream
[C#]
public virtual Stream EndGetRequestStream(IAsyncResultasyncResult);
[C++]
public: virtual Stream* EndGetRequestStream(IAsyncResult* asyncResult);
[JScript]
public function EndGetRequestStream(
   asyncResult : IAsyncResult) : Stream;

パラメータ

  • asyncResult
    ストリームの保留中の要求を参照する IAsyncResult

戻り値

データを書き込む Stream

例外

例外の種類 条件
NotSupportedException メソッドが派生クラスでオーバーライドされていないのに、そのメソッドへのアクセスが試行されました。

解説

EndGetRequestStream メソッドは、 BeginGetRequestStream メソッドが開始したストリームの非同期要求を完了します。

メモ   ガベージ コレクションのタイミングに関する問題を回避するには、 EndGetResponse を呼び出した後に、 GetResponseStream によって返されるストリームの Close メソッドを呼び出して、応答ストリームを閉じてください。

メモ    WebRequest クラスは、抽象 (Visual Basic では MustInherit) クラスです。実行時の WebRequest インスタンスの実際の動作は、 WebRequest.Create メソッドで返される派生クラスによって決まります。既定値および例外の詳細については、 HttpWebRequestFileWebRequest などの派生クラスの説明を参照してください。

使用例

[Visual Basic, C#, C++] EndGetRequestStream を呼び出して、要求ストリームを取得および使用する例を次に示します。 EndGetRequestStream メソッドは、 BeginGetRequestStream への非同期呼び出しを完了します。

 

        ' Create a new request to the mentioned URL.
            Dim myWebRequest As WebRequest = WebRequest.Create("https://www.contoso.com/codesnippets/next.asp")

            ' Create an instance of the RequestState and assign 'myWebRequest' to it's request field.            
            Dim myRequestState As New RequestState()
            myRequestState.request = myWebRequest
            myWebRequest.ContentType = "application/x-www-form-urlencoded"

            ' Set the 'Method' prperty  to 'POST' to post data to a Uri.
            myRequestState.request.Method = "POST"
            myRequestState.request.ContentType = "application/x-www-form-urlencoded"

            ' Start the Asynchronous 'BeginGetRequestStream' method call.    
            Dim r As IAsyncResult = CType(myWebRequest.BeginGetRequestStream(AddressOf ReadCallback, myRequestState), IAsyncResult)

            ' Assign the response object of 'WebRequest' to a 'WebResponse' variable.
            Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
            Console.WriteLine(ControlChars.Cr + "The string entered has been successfully posted to the Uri")
            Console.WriteLine("Please wait for the response.......")
            Dim streamResponse As Stream = myWebResponse.GetResponseStream()
            Dim streamRead As New StreamReader(streamResponse)
            Dim readBuff(256) As [Char]
            Dim count As Integer = streamRead.Read(readBuff, 0, 256)
            Console.WriteLine(ControlChars.Cr + "The contents of the HTML page are ")
            While count > 0
                Dim outputData As New [String](readBuff, 0, count)
                Console.WriteLine(outputData)
                count = streamRead.Read(readBuff, 0, 256)
            End While

           ' Close the Stream Object.
        streamResponse.Close()
        streamRead.Close()
        allDone.WaitOne()    

        ' Release the HttpWebResponse Resource.
         myWebResponse.Close()    
        Catch e As WebException
            Console.WriteLine(ControlChars.Cr + "WebException Caught!")
            Console.WriteLine("Message :{0}", e.Message)
        Catch e As Exception
            Console.WriteLine(ControlChars.Cr + "Exception Caught!")
            Console.WriteLine("Message :{0}", e.Message)
        End Try
    End Sub ' Main
     
    Private Shared Sub ReadCallback(asynchronousResult As IAsyncResult)
        Try

            ' State of request is set to asynchronous.
            Dim myRequestState As RequestState = CType(asynchronousResult.AsyncState, RequestState)
            Dim myWebRequest2 As WebRequest = myRequestState.request

            ' End of the Asynchronus request.
            Dim streamResponse As Stream = myWebRequest2.EndGetRequestStream(asynchronousResult)

            ' Create a string that is to be posted to the uri.
            Console.WriteLine(ControlChars.Cr + "Please enter a string to be posted to (https://www.contoso.com/codesnippets/next.asp) Uri:")
            Dim inputData As String = Console.ReadLine()
            Dim postData As String = "firstone" + ChrW(61) + inputData
            Dim encoder As New ASCIIEncoding()

            ' Convert  the string into a byte array.
            Dim ByteArray As Byte() = encoder.GetBytes(postData)

            ' Write data to the stream.
            streamResponse.Write(ByteArray, 0, postData.Length)
            streamResponse.Close()
            allDone.Set()

        Catch e As WebException
            Console.WriteLine(ControlChars.Cr + "WebException Caught!")
            Console.WriteLine("Message :{0}", e.Message)
        Catch e As Exception
            Console.WriteLine(ControlChars.Cr + "Exception Caught!")
            Console.WriteLine("Message :{0}", e.Message)
        End Try

    End Sub ' ReadCallback 

[C#] 
// Create a new request to the mentioned URL.    
WebRequest myWebRequest= WebRequest.Create("https://www.contoso.com");

// Create an instance of the RequestState and assign 'myWebRequest' to it's request field.    
RequestState myRequestState = new RequestState();
myRequestState.request = myWebRequest;            
myWebRequest.ContentType="application/x-www-form-urlencoded";

// Set the 'Method' prperty  to 'POST' to post data to a Uri.
myRequestState.request.Method="POST";
myRequestState.request.ContentType="application/x-www-form-urlencoded";

// Start the Asynchronous 'BeginGetRequestStream' method call.    
IAsyncResult r=(IAsyncResult) myWebRequest.BeginGetRequestStream(new AsyncCallback(ReadCallback),myRequestState);            

// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse=myWebRequest.GetResponse();
Console.WriteLine("\nThe string entered has been successfully posted to the Uri");    
Console.WriteLine("Please wait for the response.......");

Stream streamResponse=myWebResponse.GetResponseStream();
StreamReader streamRead = new StreamReader( streamResponse );
Char[] readBuff = new Char[256];
int count = streamRead.Read( readBuff, 0, 256 );
Console.WriteLine("\nThe contents of the HTML page are ");    

while (count > 0) 
{
    String outputData = new String(readBuff, 0, count);
    Console.Write(outputData);
    count = streamRead.Read(readBuff, 0, 256);
}

// Close the Stream Object.
streamResponse.Close();
streamRead.Close();
allDone.WaitOne();    

// Release the HttpWebResponse Resource.
myWebResponse.Close();            

        }
        catch(Exception e)
        {
Console.WriteLine(e.ToString());
        }

    }
    private static void ReadCallback(IAsyncResult asynchronousResult)
    {    
        try
        {

// State of request is set to asynchronous.
RequestState myRequestState=(RequestState) asynchronousResult.AsyncState;
WebRequest  myWebRequest2=myRequestState.request;

// End of the Asynchronus request.
Stream streamResponse=myWebRequest2.EndGetRequestStream(asynchronousResult);

// Create a string that is to be posted to the uri.
Console.WriteLine("\nPlease enter a string to be posted to (https://www.contoso.com) Uri:");
string inputData=Console.ReadLine();
string postData="firstone="+inputData;
ASCIIEncoding encoder = new ASCIIEncoding();

// Convert  the string into a byte array.
byte[] ByteArray = encoder.GetBytes(postData);

// Write data to the stream.
streamResponse.Write(ByteArray,0,postData.Length);
streamResponse.Close();        
allDone.Set();
        }
        catch(Exception e)
        {
Console.WriteLine(e.ToString());
        }
    }

[C++] 
// Create a new request to the mentioned URL.
WebRequest* myWebRequest= WebRequest::Create(S"https://www.contoso.com");

// Create an instance of the RequestState and assign 'myWebRequest' to its request field.
RequestState* myRequestState = new RequestState();
myRequestState->request = myWebRequest;
myWebRequest->ContentType=S"application/x-www-form-urlencoded";

// Set the 'Method' prperty  to 'POST' to post data to a Uri.
myRequestState->request->Method=S"POST";
myRequestState->request->ContentType=S"application/x-www-form-urlencoded";

// Start the Asynchronous 'BeginGetRequestStream' method call.
IAsyncResult* r = 
   dynamic_cast<IAsyncResult*> (myWebRequest->BeginGetRequestStream(new AsyncCallback(r, 
   WebRequest_BeginGetRequeststream::ReadCallback), myRequestState));

// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse* myWebResponse = myWebRequest->GetResponse();
Console::WriteLine(S"\nThe String* entered has been successfully posted to the Uri");
Console::WriteLine(S"Please wait for the response.......");

Stream* streamResponse = myWebResponse->GetResponseStream();
StreamReader* streamRead = new StreamReader(streamResponse);
Char readBuff[] = new Char[256];
int count = streamRead->Read(readBuff, 0, 256);
Console::WriteLine(S"\nThe contents of the HTML page are ");

while (count > 0) {
   String* outputData = new String(readBuff, 0, count);
   Console::Write(outputData);
   count = streamRead->Read(readBuff, 0, 256);
}

// Close the Stream Object.
streamResponse->Close();
streamRead->Close();
WebRequest_BeginGetRequeststream::allDone->WaitOne();

// Release the HttpWebResponse Resource.
myWebResponse->Close();

   } catch (Exception* e) {
Console::WriteLine(e);
   }
}

[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

参照

WebRequest クラス | WebRequest メンバ | System.Net 名前空間 | BeginGetRequestStream | 非同期要求の作成