当前位置: 首页>>代码示例>>C#>>正文


C# WebRequest.BeginGetResponse方法代码示例

本文整理汇总了C#中System.Net.WebRequest.BeginGetResponse方法的典型用法代码示例。如果您正苦于以下问题:C# WebRequest.BeginGetResponse方法的具体用法?C# WebRequest.BeginGetResponse怎么用?C# WebRequest.BeginGetResponse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.WebRequest的用法示例。


在下文中一共展示了WebRequest.BeginGetResponse方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RequestState

//引入命名空间
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Threading;

public class RequestState
{
  // This class stores the state of the request.
  const int BUFFER_SIZE = 1024;
  public StringBuilder requestData;
  public byte[] bufferRead;
  public WebRequest request;
  public WebResponse response;
  public Stream responseStream;
  public RequestState()
  {
    bufferRead = new byte[BUFFER_SIZE];
    requestData = new StringBuilder("");
    request = null;
    responseStream = null;
  }
}
class WebRequest_BeginGetResponse
{
  public static ManualResetEvent allDone= new ManualResetEvent(false);
  const int BUFFER_SIZE = 1024;
  static void 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=new WebProxy("myproxy:80");

      proxy.Credentials=new NetworkCredential("srikun","simrin123");
      myWebRequest.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.Message);
      Console.WriteLine("\n{0}",e.Status);
    } 
    catch(Exception e)
    {
      Console.WriteLine("Exception raised!");
      Console.WriteLine("Source : " + e.Source);
      Console.WriteLine("Message : " + e.Message);
    }
  }
  private static void RespCallback(IAsyncResult asynchronousResult)
  {  
    try
    {
      // Set the State of request to asynchronous.
      RequestState myRequestState=(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, new 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 : " + e.Source);
      Console.WriteLine("Message : " + e.Message);
    }
  }
  private static  void ReadCallBack(IAsyncResult asyncResult)
  {
    try
    {
      // Result state is set to AsyncState.
      RequestState myRequestState = (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, new 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);
    }
  }
}
开发者ID:.NET开发者,项目名称:System.Net,代码行数:134,代码来源:WebRequest.BeginGetResponse


注:本文中的System.Net.WebRequest.BeginGetResponse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。