本文档已存档,并且将不进行维护。

HttpWebRequest.EndGetResponse 方法

结束对 Internet 资源的异步请求。

命名空间:System.Net
程序集:System(在 system.dll 中)

public override WebResponse EndGetResponse (
	IAsyncResult asyncResult
)
public WebResponse EndGetResponse (
	IAsyncResult asyncResult
)
public override function EndGetResponse (
	asyncResult : IAsyncResult
) : WebResponse

参数

asyncResult

挂起的对响应的请求。

返回值

包含来自 Internet 资源的响应的 WebResponse

异常类型条件

ArgumentNullException

asyncResult 为 空引用(在 Visual Basic 中为 Nothing)。

InvalidOperationException

以前使用 asyncResult. 调用过此方法。

- 或 -

ContentLength 属性大于 0,但是数据尚未写入请求流。

WebException

Abort 以前被调用过。

- 或 -

处理请求时发生错误。

ArgumentException

当前实例没有从 BeginGetResponse 调用返回 asyncResult

EndGetResponse 方法完成对通过调用 BeginGetResponse 方法启动的 Internet 资源的异步请求。

Caution note警告

必须调用 Close 方法关闭该流并释放连接。如果未能做到这一点,可能导致应用程序用完连接。

Note注意

当在应用程序中启用网络跟踪功能后,此成员将输出跟踪信息。有关更多信息,请参见 网络跟踪

下面的代码示例使用 EndGetResponse 方法结束对 Internet 资源的异步请求。

  try
  {      

    
    // Create a HttpWebrequest object to the desired URL.
    HttpWebRequest myHttpWebRequest1= (HttpWebRequest)WebRequest.Create("http://www.contoso.com");
    
    // 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);

    allDone.WaitOne();
    
    // Release the HttpWebResponse resource.
    myRequestState.response.Close();
  }
  catch(WebException e)
  {
    Console.WriteLine("\nException raised!");
    Console.WriteLine("\nMessage:{0}",e.Message);
    Console.WriteLine("\nStatus:{0}",e.Status);
    Console.WriteLine("Press any key to continue..........");
  }
  catch(Exception e)
  {
    Console.WriteLine("\nException raised!");
    Console.WriteLine("Source :{0} " , e.Source);
    Console.WriteLine("Message :{0} " , e.Message);
    Console.WriteLine("Press any key to continue..........");
    Console.Read();
  }
}
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)
  {
    Console.WriteLine("\nException raised!");
    Console.WriteLine("\nMessage:{0}",e.Message);
    Console.WriteLine("\nStatus:{0}",e.Status);
    
  }
}
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 print it 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, new AsyncCallback(ReadCallBack), myRequestState);
  }
  else
  {
    Console.WriteLine("\nThe contents of the Html page are : ");
    if(myRequestState.requestData.Length>1)
    {
      string stringContent;
      stringContent = myRequestState.requestData.ToString();
      Console.WriteLine(stringContent);
    }
    Console.WriteLine("Press any key to continue..........");
    Console.ReadLine();
    
    responseStream.Close();
    allDone.Set();
   
  }

  }
  catch(WebException e)
  {
    Console.WriteLine("\nException raised!");
    Console.WriteLine("\nMessage:{0}",e.Message);
    Console.WriteLine("\nStatus:{0}",e.Status);
    
  }

}

        try {
            // Create a HttpWebrequest object to the desired URL.
            HttpWebRequest myHttpWebRequest1 = ((HttpWebRequest)
                (WebRequest.Create("http://www.contoso.com")));
            // 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));
            allDone.WaitOne();
            // Release the HttpWebResponse resource.
            myRequestState.response.Close();
        }
        catch (WebException e) {
            Console.WriteLine("\nException raised!");
            Console.WriteLine("\nMessage:{0}", e.get_Message());
            Console.WriteLine("\nStatus:{0}", e.get_Status());
            Console.WriteLine("Press any key to continue..........");
        }
        catch (System.Exception e) {
            Console.WriteLine("\nException raised!");
            Console.WriteLine("Source :{0} ", e.get_Source());
            Console.WriteLine("Message :{0} ", e.get_Message());
            Console.WriteLine("Press any key to continue..........");
            Console.Read();
        }
    } //main

    private static void RespCallback(IAsyncResult asynchronousResult)
    {
        try {
            // State of request is asynchronous.
            RequestState myRequestState = (RequestState)
                (asynchronousResult.get_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) {
            Console.WriteLine("\nException raised!");
            Console.WriteLine("\nMessage:{0}", e.get_Message());
            Console.WriteLine("\nStatus:{0}", e.get_Status());
        }
    } //RespCallback

    private static void ReadCallBack(IAsyncResult asyncResult)
    {
        try {
            RequestState myRequestState = (RequestState)
                (asyncResult.get_AsyncState());
            Stream responseStream = myRequestState.streamResponse;
            int read = responseStream.EndRead(asyncResult);

            // Read the HTML page and then print it 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 contents of the Html page are : ");

                if (myRequestState.requestData.get_Length() > 1) {
                    String stringContent;
                    stringContent = myRequestState.requestData.ToString();
                    Console.WriteLine(stringContent);
                }
                Console.WriteLine("Press any key to continue..........");
                Console.ReadLine();
                responseStream.Close();
                allDone.Set();
            }
        }
        catch (WebException e) {
            Console.WriteLine("\nException raised!");
            Console.WriteLine("\nMessage:{0}", e.get_Message());
            Console.WriteLine("\nStatus:{0}", e.get_Status());
        }
    } //ReadCallBack
} //HttpWebRequest_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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0
显示: