Export (0) Print
Expand All

WebRequest.EndGetResponse Method

When overridden in a descendant class, returns a WebResponse.

Namespace: System.Net
Assembly: System (in system.dll)

public:
virtual WebResponse^ EndGetResponse (
	IAsyncResult^ asyncResult
)
public WebResponse EndGetResponse (
	IAsyncResult asyncResult
)
public function EndGetResponse (
	asyncResult : IAsyncResult
) : WebResponse

Parameters

asyncResult

An IAsyncResult that references a pending request for a response.

Return Value

A WebResponse that contains a response to the Internet request.

Exception typeCondition

NotImplementedException

Any attempt is made to access the method, when the method is not overridden in a descendant class.

The EndGetResponse method completes an asynchronous request for an Internet resource that was started with the BeginGetResponse method.

NoteNote

The WebRequest class is an abstract class. The actual behavior of WebRequest instances at run time is determined by the descendant class returned by the System.Net.WebRequest.Create method. For more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.

The following example calls the EndGetResponse to retrieve the target resource.

#using <System.dll>

using namespace System;
using namespace System::Net;
using namespace System::IO;
using namespace System::Text;
using namespace System::Threading;
public ref class RequestState
{
private:

   // This class stores the state of the request.
   literal int BUFFER_SIZE = 1024;

public:
   StringBuilder^ requestData;
   array<Byte>^bufferRead;
   WebRequest^ request;
   WebResponse^ response;
   Stream^ responseStream;
   RequestState()
   {
      bufferRead = gcnew array<Byte>(BUFFER_SIZE);
      requestData = gcnew StringBuilder( "" );
      request = nullptr;
      responseStream = nullptr;
   }

};

ref class WebRequest_BeginGetResponse
{
public:
   static ManualResetEvent^ allDone = gcnew ManualResetEvent( false );
   literal int BUFFER_SIZE = 1024;
   static void RespCallback( IAsyncResult^ asynchronousResult )
   {
      try
      {
         
         // Set the State of request to asynchronous.
         RequestState^ myRequestState = dynamic_cast<RequestState^>(asynchronousResult->AsyncState);
         WebRequest^ myWebRequest1 = myRequestState->request;
         
         // End the Asynchronous response.
         myRequestState->response = myWebRequest1->EndGetResponse( asynchronousResult );
         
         // Read the response into a 'Stream' object.
         Stream^ responseStream = myRequestState->response->GetResponseStream();
         myRequestState->responseStream = responseStream;
         
         // Begin the reading of the contents of the HTML page and print it to the console.
         IAsyncResult^ asynchronousResultRead = responseStream->BeginRead( myRequestState->bufferRead, 0, BUFFER_SIZE, gcnew AsyncCallback( ReadCallBack ), myRequestState );
      }
      catch ( WebException^ e ) 
      {
         Console::WriteLine( "WebException raised!" );
         Console::WriteLine( "\n {0}", e->Message );
         Console::WriteLine( "\n {0}", e->Status );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "Exception raised!" );
         Console::WriteLine( "Source : {0}", e->Source );
         Console::WriteLine( "Message : {0}", e->Message );
      }

   }

   static void ReadCallBack( IAsyncResult^ asyncResult )
   {
      try
      {
         
         // Result state is set to AsyncState.
         RequestState^ myRequestState = dynamic_cast<RequestState^>(asyncResult->AsyncState);
         Stream^ responseStream = myRequestState->responseStream;
         int read = responseStream->EndRead( asyncResult );
         
         // Read the contents of the HTML page and then print to the console.
         if ( read > 0 )
         {
            myRequestState->requestData->Append( Encoding::ASCII->GetString( myRequestState->bufferRead, 0, read ) );
            IAsyncResult^ asynchronousResult = responseStream->BeginRead( myRequestState->bufferRead, 0, BUFFER_SIZE, gcnew AsyncCallback( ReadCallBack ), myRequestState );
         }
         else
         {
            Console::WriteLine( "\nThe HTML page Contents are:  " );
            if ( myRequestState->requestData->Length > 1 )
            {
               String^ sringContent;
               sringContent = myRequestState->requestData->ToString();
               Console::WriteLine( sringContent );
            }
            Console::WriteLine( "\nPress 'Enter' key to continue........" );
            responseStream->Close();
            allDone->Set();
         }
      }
      catch ( WebException^ e ) 
      {
         Console::WriteLine( "WebException raised!" );
         Console::WriteLine( "\n {0}", e->Message );
         Console::WriteLine( "\n {0}", e->Status );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "Exception raised!" );
         Console::WriteLine( "Source : {0}", e->Source );
         Console::WriteLine( "Message : {0}", e->Message );
      }

   }

};

int main()
{
   try
   {
      
      // Create a new webrequest to the mentioned URL.
      WebRequest^ myWebRequest = WebRequest::Create( "http://www.contoso.com" );
      
      // Please, set the proxy to a correct value.
      WebProxy^ proxy = gcnew WebProxy( "myproxy:80" );
      proxy->Credentials = gcnew NetworkCredential( "srikun","simrin123" );
      myWebRequest->Proxy = proxy;
      
      // Create a new instance of the RequestState.
      RequestState^ myRequestState = gcnew RequestState;
      
      // The 'WebRequest' object is associated to the 'RequestState' object.
      myRequestState->request = myWebRequest;
      
      // Start the Asynchronous call for response.
      IAsyncResult^ asyncResult = dynamic_cast<IAsyncResult^>(myWebRequest->BeginGetResponse( gcnew AsyncCallback( WebRequest_BeginGetResponse::RespCallback ), myRequestState ));
      WebRequest_BeginGetResponse::allDone->WaitOne();
      
      // Release the WebResponse resource.
      myRequestState->response->Close();
      Console::Read();
   }
   catch ( WebException^ e ) 
   {
      Console::WriteLine( "WebException raised!" );
      Console::WriteLine( "\n {0}", e->Message );
      Console::WriteLine( "\n {0}", e->Status );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception raised!" );
      Console::WriteLine( "Source : {0}", e->Source );
      Console::WriteLine( "Message : {0}", e->Message );
   }

}


import System.*;
import System.Net.*;
import System.IO.*;
import System.Text.*;
import System.Threading.*;

public class RequestState
{
    // This class stores the state of the request.
    private static final int BUFFER_SIZE = 1024;
    public StringBuilder requestData;
    public ubyte bufferRead[];
    public WebRequest request;
    public WebResponse response;
    public Stream responseStream;

    public RequestState()
    {
        bufferRead = new ubyte[BUFFER_SIZE];
        requestData = new StringBuilder("");
        request = null;
        responseStream = null;
    } //RequestState
} //RequestState

class WebRequest_BeginGetResponse
{
    public static ManualResetEvent allDone = new ManualResetEvent(false);
    private static final int BUFFER_SIZE = 1024;

    public static void main(String[] args)
    {
        try {
            // Create a new webrequest to the mentioned URL.   
            WebRequest myWebRequest = 
                WebRequest.Create("http://www.contoso.com");
            // Please, set the proxy to a correct value. 
            WebProxy proxy = new WebProxy("myproxy:80");

            proxy.set_Credentials(new NetworkCredential("srikun", "simrin123"));
            myWebRequest.set_Proxy(proxy);
            // Create a new instance of the RequestState.
            RequestState myRequestState = new RequestState();
            // The 'WebRequest' object is associated to the 'RequestState' object.
            myRequestState.request = myWebRequest;
            // Start the Asynchronous call for response.
            IAsyncResult asyncResult = (IAsyncResult)(myWebRequest.
                BeginGetResponse(new AsyncCallback(RespCallback),
                myRequestState));
            allDone.WaitOne();
            // Release the WebResponse resource.
            myRequestState.response.Close();
            Console.Read();
        }
        catch (WebException e) {
            Console.WriteLine("WebException raised!");
            Console.WriteLine("\n{0}", e.get_Message());
            Console.WriteLine("\n{0}", e.get_Status());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception raised!");
            Console.WriteLine("Source : " + e.get_Source());
            Console.WriteLine("Message : " + e.get_Message());
        }
    } //main

    private static void RespCallback(IAsyncResult asynchronousResult)
    {
        try {
            // Set the State of request to asynchronous.
            RequestState myRequestState = (RequestState)(
                asynchronousResult.get_AsyncState());
            WebRequest myWebRequest1 = myRequestState.request;
            // End the Asynchronous response.
            myRequestState.response = myWebRequest1.EndGetResponse(
                asynchronousResult);
            // Read the response into a 'Stream' object.
            Stream responseStream = myRequestState.response.GetResponseStream();
            myRequestState.responseStream = responseStream;
            // Begin the reading of the contents of the HTML page and print it 
            // to the console.
            IAsyncResult asynchronousResultRead = responseStream.BeginRead(
                myRequestState.bufferRead, 0, BUFFER_SIZE, new AsyncCallback(
                ReadCallBack), myRequestState);
        }
        catch (WebException e) {
            Console.WriteLine("WebException raised!");
            Console.WriteLine("\n{0}", e.get_Message());
            Console.WriteLine("\n{0}", e.get_Status());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception raised!");
            Console.WriteLine("Source : " + e.get_Source());
            Console.WriteLine("Message : " + e.get_Message());
        }
    } //RespCallback

    private static void ReadCallBack(IAsyncResult asyncResult)
    {
        try {
            // Result state is set to AsyncState.
            RequestState myRequestState = (RequestState)(
                asyncResult.get_AsyncState());
            Stream responseStream = myRequestState.responseStream;
            int read = responseStream.EndRead(asyncResult);
            // Read the contents of the HTML page and then print to the console.
            if (read > 0) {
                myRequestState.requestData.Append(Encoding.get_ASCII().
                    GetString(myRequestState.bufferRead, 0, read));
                IAsyncResult asynchronousResult = responseStream.BeginRead(
                    myRequestState.bufferRead, 0, BUFFER_SIZE,
                    new AsyncCallback(ReadCallBack), myRequestState);
            }
            else {
                Console.WriteLine("\nThe HTML page Contents are:  ");
                if (myRequestState.requestData.get_Length() > 1) {
                    String sringContent;
                    sringContent = myRequestState.requestData.ToString();
                    Console.WriteLine(sringContent);
                }
                Console.WriteLine("\nPress 'Enter' key to continue........");
                responseStream.Close();
                allDone.Set();
            }
        }
        catch (WebException e) {
            Console.WriteLine("WebException raised!");
            Console.WriteLine("\n{0}", e.get_Message());
            Console.WriteLine("\n{0}", e.get_Status());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception raised!");
            Console.WriteLine("Source : {0}", e.get_Source());
            Console.WriteLine("Message : {0}", e.get_Message());
        }
    } //ReadCallBack
} //WebRequest_BeginGetResponse

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft