要查看英语原文,请勾选“英语”复选框。也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
翻译
英语

HttpWebRequest.BeginGetResponse 方法

开始对 Internet 资源的异步请求。

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

public override IAsyncResult BeginGetResponse(
	AsyncCallback callback,
	Object state
)

参数

callback
类型:System.AsyncCallback
AsyncCallback 委托
state
类型:System.Object
此请求的状态对象。

返回值

类型:System.IAsyncResult
引用对响应的异步请求的 IAsyncResult

异常条件
InvalidOperationException

流正由上一个 BeginGetResponse 调用使用

- 或 -

线程池中的线程即将用完。

NotImplementedException

此方法未实现。

NotSupportedException

callback 参数为 null

ProtocolViolationException

Method 为 GET。

WebException

Abort 以前被调用过。

BeginGetResponse 方法开始对 Internet 资源响应的异步请求。 异步回调方法使用 EndGetResponse 方法返回实际的 WebResponse

如果引发 WebException,请使用该异常的 ResponseStatus 属性确定服务器的响应。

在异步请求中,客户端应用程序负责实现自己的超时机制。

BeginGetResponse 方法返回的 IAsyncResult 不支持 AsyncWaitHandle 属性。 如果某个应用程序尝试访问 AsyncWaitHandle 属性,则会引发 NotSupportedException

AllowReadStreamBuffering 属性影响调用 BeginGetResponse 方法中的回调的时间。 AllowReadStreamBuffering 属性为 true 时,将整个流下载到内存后将引发回调。 如果 AllowReadStreamBuffering 属性为 false,则当有流可供读取时(可能在所有数据到达之前)将引发回调。


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("http://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
  }

}


Silverlight

受以下版本支持:5、4、3

Silverlight for Windows Phone

受以下版本支持:Windows Phone OS 7.1、Windows Phone OS 7.0

XNA Framework

受以下版本支持:Windows Phone OS 7.0

有关 Silverlight 支持的操作系统和浏览器的列表,请参见 支持的操作系统和浏览器

社区附加资源

添加
显示: