当前位置:武林网文章中心语言编程HTML、.Net、AJAX → C# 刚写的 HttpRequsetResponse 类,异步、事件

C# 刚写的 HttpRequsetResponse 类,异步、事件

减小字体 增大字体 作者:C# 刚写的 HttpRequsetResponse 类,异步、事件  来源:www.hack50.com  发布时间:2014-9-10 22:00:05
//调用示例:
private void button1_Click(object sender, System.EventArgs e)
{
   HttpRequestResponse xx = new HttpRequestResponse();
   xx.DataArrival += new DataArrivalEventHandler(xx_DataArrival);
   xx.SendRequest("http://www.triaton.com.cn/dotnet/tree/index.asp","userid=老","Post");
   xx.SendRequest("http://www.triaton.com.cn/dotnet/tree/index.asp","userid=老","get");
}
private void xx_DataArrival(object sender, DataArrivalEventArgs e)
{
   Console.WriteLine(e.RecievedData);
   Console.WriteLine(e.IsComplete);
}
//========================
//Class1.cs
using System;
using System.Net;
using System.Threading;
using System.Text;
using System.IO;

public class RequestState
{
   const int BUFFER_SIZE = 1024;
   public System.Text.StringBuilder RequestData;
   public byte[] BufferRead;
   public System.Net.HttpWebRequest Request;
   public System.IO.Stream ResponseStream;
   public System.Text.Decoder StreamDecode = System.Text.Encoding.GetEncoding("gb2312").GetDecoder();
   
   public RequestState()
   {
\tBufferRead = new byte[BUFFER_SIZE];
\tRequestData = new System.Text.StringBuilder("");
\tRequest = null;
\tResponseStream = null;
   }
}
public delegate void DataArrivalEventHandler(object sender, DataArrivalEventArgs e);
public class DataArrivalEventArgs : System.EventArgs
{
   private string mRecievedData;
   private bool mIsComplete = false;
   public DataArrivalEventArgs(string Data,bool Complete)
   {
\tmRecievedData = Data;
\tmIsComplete = Complete;
   }
   public string RecievedData
   {
\tget
\t{
\t    return mRecievedData;
\t}
   }
   public bool IsComplete
   {
\tget
\t{
\t    return mIsComplete;
\t}
   }
}


class HttpRequestResponse
{
   public static System.Threading.ManualResetEvent allDone = new System.Threading.ManualResetEvent(false);
   const int BUFFER_SIZE = 1024;
   public event DataArrivalEventHandler DataArrival;
   
   protected virtual void OnDataArrival(DataArrivalEventArgs e)
   {
\tif (DataArrival != null)
\t{
\t    DataArrival(this, e);
\t}
   }
   private System.Text.Encoding mRequestEncoding = System.Text.Encoding.GetEncoding("gb2312");
   public void SendRequest(string RequestURL,string RequestData,string RequestMethod)
   {
\tHttpWebRequest req;
\tRequestState rs = new RequestState();
\tif (RequestData != null)
\t{
\t    int i=0, j;
\t    Char[] reserved = {''?'', ''='', ''&''};
\t    while(i<RequestData.Length)
\t    {
\t\tj=RequestData.IndexOfAny(reserved, i);
\t\tif (j==-1)
\t\t{
\t\t    rs.RequestData.Append(System.Web.HttpUtility.UrlEncode(RequestData.Substring(i, RequestData.Length-i),mRequestEncoding));
\t\t    break;
\t\t}
\t\trs.RequestData.Append(System.Web.HttpUtility.UrlEncode(RequestData.Substring(i, j-i),mRequestEncoding));
\t\trs.RequestData.Append(RequestData.Substring(j,1));
\t\ti = j+1;
\t    }
\t}
\tif (RequestMethod.ToUpper() == "POST")
\t{
\t    byte[] Buffer = null;
\t    Buffer = mRequestEncoding.GetBytes(rs.RequestData.ToString());
\t    req =(HttpWebRequest) WebRequest.Create(RequestURL);
\t    req.ContentLength = Buffer.Length;
\t    req.Method = RequestMethod;
\t    req.ContentType = "application/x-www-form-urlencoded";
\t    Stream newStream = req.GetRequestStream();
\t    newStream.Write(Buffer, 0, Buffer.Length);
\t    newStream.Close();
\t}
\telse
\t{
\t    req =(HttpWebRequest) WebRequest.Create(RequestURL + "?" + rs.RequestData.ToString());
\t}

\trs.Request = req;
\tSystem.IAsyncResult r = (System.IAsyncResult) req.BeginGetResponse(new System.AsyncCallback(ResponseCallback), rs);
   }

   private void ResponseCallback(IAsyncResult ar)
   {
\tRequestState rs = (RequestState) ar.AsyncState;

\tSystem.Net.HttpWebRequest req = rs.Request;

\tSystem.Net.HttpWebResponse resp = (System.Net.HttpWebResponse) req.EndGetResponse(ar);

\tSystem.IO.Stream ResponseStream = resp.GetResponseStream();

\trs.ResponseStream = ResponseStream;

\tIAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs);
   }

   private void ReadCallBack(IAsyncResult asyncResult)
   {
\t
\tRequestState rs = (RequestState)asyncResult.AsyncState;

\tSystem.IO.Stream responseStream = rs.ResponseStream;
\t
\tint read = responseStream.EndRead( asyncResult );

\tif (read > 0)
\t{
\t    Char[] charBuffer = new Char[BUFFER_SIZE];

\t    int len = rs.StreamDecode.GetChars( rs.BufferRead, 0, read, charBuffer, 0 );
\t    System.String str = new System.String( charBuffer, 0, len);

\t    OnDataArrival(new DataArrivalEventArgs(str,false));

\t    rs.RequestData.Append(str);

\t    IAsyncResult ar = responseStream.BeginRead( rs.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs);
\t}
\telse
\t{
\t    if(rs.RequestData.Length>1)
\t    {
\t\tstring strContent;
\t\tstrContent = rs.RequestData.ToString();
\t    }
\t    responseStream.Close();
\t    OnDataArrival(new DataArrivalEventArgs("\n",true));
\t    allDone.Set();
\t}
\treturn;
   }

}

本文引用网址:

在下列搜索引擎中搜索“C# 刚写的 HttpRequsetResponse 类,异步、事件”的相关信息:

谷歌搜索 百度搜索 360搜索 雅虎搜索 搜狗搜索 搜搜搜索 必应搜索 有道搜索
你可能还喜欢以下文章
  • 1怎么做爱舒服?真人示范做爱姿...
  • 2合肥艳照门QQ空间曝光男主角与...
  • 3男女做爱技巧图解 真人示范做爱...
  • 4军艺校花徐冬冬全裸高清组图 徐...
  • 5最新野战门事件完整不雅照大全...
  • 1女孩下面痒了怎么办?
  • 2一组超级黄色笑话
  • 3十八岁女孩第一次做鸡的感觉
  • 4教你如何盗QQ密码
  • 5日本AV六级试卷 (附标准答案)
  • 14D肉蒲团之扶桑千人斩qvod高清...
  • 2林晨钰艳照qvod完整版 林晨钰秀...
  • 3最新王梦溪未经处理雅照曝光 兰...
  • 4武汉工程大学林晨钰爆奶门 35张...
  • 5四川美院女生遭前男友发艳照报...
  • 1怎样破解别人的微信密码?手机...
  • 2怎样翻墙上网?什么是翻墙上网...
  • 3如何破解qq密保问题答案?怎么...
  • 4怎样破解无权限访问受限?进入...
  • 5苹果5开机密码怎么破解?iphon...
  • 赞助商广告

    图片文章导读