Partager via


HttpWebRequest.EndGetResponse Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Ends an asynchronous request to an Internet resource.

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

Syntax

'Declaration
Public Overrides Function EndGetResponse ( _
    asyncResult As IAsyncResult _
) As WebResponse
public override WebResponse EndGetResponse(
    IAsyncResult asyncResult
)

Parameters

Return Value

Type: System.Net.WebResponse
A WebResponse that contains the response from the Internet resource.

Exceptions

Exception Condition
ArgumentNullException

asyncResult is nulla null reference (Nothing in Visual Basic).

InvalidOperationException

This method was called previously using asyncResult.

NotImplementedException

This method is not implemented.

WebException

Abort was previously called.

-or-

An error occurred while processing the request.

ArgumentException

asyncResult was not returned by the current instance from a call to BeginGetResponse.

Remarks

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

Caution noteCaution:

You must call the WebResponse method to close the stream and release the connection. Failure to do so may cause your application to run out of connections.

Examples

public class RequestState
{
  // This class stores the State of the request.
  const int BUFFER_SIZE = 1024;
  public StringBuilder requestData;
  public byte[] BufferRead;
  public HttpWebRequest request;
  public HttpWebResponse response;
  public Stream streamResponse;

  public RequestState()
  {
    BufferRead = new byte[BUFFER_SIZE];
    requestData = new StringBuilder("");
    request = null;
    streamResponse = null;
  }
}

public class Example
{

  public static ManualResetEvent allDone= new ManualResetEvent(false);
  const int BUFFER_SIZE = 1024;

  public static void Demo(System.Windows.Controls.TextBlock outputBlock)
  {
  try
  {      

    System.Uri uri = new Uri("https://www.contoso.com");
    // Create a HttpWebrequest object to the desired URL.
    HttpWebRequest myHttpWebRequest1= (HttpWebRequest)WebRequest.Create(uri);

    // Create an instance of the RequestState and assign the previous myHttpWebRequest1
    // object to it's request field.  
    RequestState myRequestState = new RequestState();  
    myRequestState.request = myHttpWebRequest1;


    // Start the asynchronous request.
    IAsyncResult result=
      (IAsyncResult) myHttpWebRequest1.BeginGetResponse(new AsyncCallback(RespCallback),myRequestState);

  }
  catch(WebException e)
  {
    outputBlock.Text += "\nException raised!\n";
    outputBlock.Text += "Message: ";
    outputBlock.Text += e.Message;
    outputBlock.Text += "\nStatus: ";
    outputBlock.Text += e.Status;
    outputBlock.Text += "\n";
  }
  catch(Exception e)
  {
    outputBlock.Text += "\nException raised!\n";
    outputBlock.Text += "\nMessage: ";
    outputBlock.Text += e.Message;
    outputBlock.Text += "\n";
  }
}
private static void RespCallback(IAsyncResult asynchronousResult)
{  
  try
  {
    // State of request is asynchronous.
    RequestState myRequestState=(RequestState) asynchronousResult.AsyncState;
    HttpWebRequest  myHttpWebRequest2=myRequestState.request;
    myRequestState.response = (HttpWebResponse) myHttpWebRequest2.EndGetResponse(asynchronousResult);

    // Read the response into a Stream object.
    Stream responseStream = myRequestState.response.GetResponseStream();
    myRequestState.streamResponse=responseStream;

    // Begin the Reading of the contents of the HTML page and print it to the console.
    IAsyncResult asynchronousInputRead = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);
  }
  catch(WebException e)
  {
    // Need to handle the exception
  }
}
private static  void ReadCallBack(IAsyncResult asyncResult)
{
  try
  {

    RequestState myRequestState = (RequestState)asyncResult.AsyncState;
    Stream responseStream = myRequestState.streamResponse;
    int read = responseStream.EndRead( asyncResult );
    // Read the HTML page and then do something with it
    if (read > 0)
    {
      myRequestState.requestData.Append(Encoding.UTF8.GetString(myRequestState.BufferRead, 0, read));
      IAsyncResult asynchronousResult = responseStream.BeginRead( myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);
    }
    else
    {
      if(myRequestState.requestData.Length>1)
      {
        string stringContent;
        stringContent = myRequestState.requestData.ToString();
        // do something with the response stream here
      }

      responseStream.Close();
      allDone.Set();

    }

  }
  catch(WebException e)
  {
    // Need to handle the exception
  }

}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.