public class RequestState
{
const int BUFFER_SIE = 102400;
public StringBuilder RequestData;
public byte[] BufferRead;
public HttpWebRequest Request;
public Stream ResponseStream;
public Decoder StreamDecode = Encoding.UTF8.GetDecoder();
public RequestState()
{
BufferRead = new byte[BUFFER_SIE];
RequestData = new StringBuilder("");
Request = null;
ResponseStream = null;
}
}
public class ClientGetAsync
{
public static ManualResetEvent allDone = new ManualResetEvent(false);
const int BUFFER_SIE=102400;
public static void Main(string[] args)
{
if (args.Length < 1)
{
showUsage();
// return;
}
Uri HttpSite = new Uri(" http://www.baidu.com");
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(HttpSite);
RequestState rs = new RequestState();
rs.Request = wreq;
IAsyncResult r = (IAsyncResult)wreq.BeginGetResponse(new AsyncCallback(RespCallback),rs);
allDone.WaitOne();
Console.ReadKey();
Console.ReadLine();
}
public static void showUsage()
{
Console.WriteLine("Try get a URL");
Console.WriteLine("\r\n usage::");
Console.WriteLine("ClientGetAsync URL");
Console.WriteLine("Case::");
Console.WriteLine("ClientGetAsync http://www.microsoft.com/net/");
}
private static void RespCallback(IAsyncResult ar)
{
RequestState rs = (RequestState)ar.AsyncState;
HttpWebRequest req = rs.Request;
Thread.Sleep(5000);
HttpWebResponse resp = (HttpWebResponse)req.EndGetResponse(ar);
Stream ResponseStream = resp.GetResponseStream();
rs.ResponseStream = ResponseStream;
IAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIE, new AsyncCallback(ReadCallBack), rs);
}
private static void ReadCallBack(IAsyncResult asyncResult)
{
RequestState rs = (RequestState)asyncResult.AsyncState;
Stream responseStream = rs.ResponseStream;
int read = responseStream.EndRead(asyncResult);
if (read > 0)
{
char[] charBuffer = new Char[BUFFER_SIE];
int len = rs.StreamDecode.GetChars(rs.BufferRead, 0, read, charBuffer, 0);
String str = new String(charBuffer, 0, len);
rs.RequestData.Append(str);
IAsyncResult ar = responseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIE, new AsyncCallback(ReadCallBack), rs);
}
else
{
if (rs.RequestData.Length > 1)
{
string strContent;
strContent = rs.RequestData.ToString();
Console.WriteLine(strContent);
}
responseStream.Close();
allDone.Set();
}
return;
}
}
{
const int BUFFER_SIE = 102400;
public StringBuilder RequestData;
public byte[] BufferRead;
public HttpWebRequest Request;
public Stream ResponseStream;
public Decoder StreamDecode = Encoding.UTF8.GetDecoder();
public RequestState()
{
BufferRead = new byte[BUFFER_SIE];
RequestData = new StringBuilder("");
Request = null;
ResponseStream = null;
}
}
public class ClientGetAsync
{
public static ManualResetEvent allDone = new ManualResetEvent(false);
const int BUFFER_SIE=102400;
public static void Main(string[] args)
{
if (args.Length < 1)
{
showUsage();
// return;
}
Uri HttpSite = new Uri(" http://www.baidu.com");
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(HttpSite);
RequestState rs = new RequestState();
rs.Request = wreq;
IAsyncResult r = (IAsyncResult)wreq.BeginGetResponse(new AsyncCallback(RespCallback),rs);
allDone.WaitOne();
Console.ReadKey();
Console.ReadLine();
}
public static void showUsage()
{
Console.WriteLine("Try get a URL");
Console.WriteLine("\r\n usage::");
Console.WriteLine("ClientGetAsync URL");
Console.WriteLine("Case::");
Console.WriteLine("ClientGetAsync http://www.microsoft.com/net/");
}
private static void RespCallback(IAsyncResult ar)
{
RequestState rs = (RequestState)ar.AsyncState;
HttpWebRequest req = rs.Request;
Thread.Sleep(5000);
HttpWebResponse resp = (HttpWebResponse)req.EndGetResponse(ar);
Stream ResponseStream = resp.GetResponseStream();
rs.ResponseStream = ResponseStream;
IAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIE, new AsyncCallback(ReadCallBack), rs);
}
private static void ReadCallBack(IAsyncResult asyncResult)
{
RequestState rs = (RequestState)asyncResult.AsyncState;
Stream responseStream = rs.ResponseStream;
int read = responseStream.EndRead(asyncResult);
if (read > 0)
{
char[] charBuffer = new Char[BUFFER_SIE];
int len = rs.StreamDecode.GetChars(rs.BufferRead, 0, read, charBuffer, 0);
String str = new String(charBuffer, 0, len);
rs.RequestData.Append(str);
IAsyncResult ar = responseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIE, new AsyncCallback(ReadCallBack), rs);
}
else
{
if (rs.RequestData.Length > 1)
{
string strContent;
strContent = rs.RequestData.ToString();
Console.WriteLine(strContent);
}
responseStream.Close();
allDone.Set();
}
return;
}
}