当前位置:编程学习 > C#/ASP.NET >>

C# 刚写的 HttpRequsetResponse 类,异步、事件... 还热乎着呢!(2003-7-1更新)

答案:1.支持Proxy
2.可指定同步或异步
3.一些小修改

//调用示例:
private void button1_Click(object sender, System.EventArgs e)
    {
        HttpRequestResponse xx = new HttpRequestResponse();
        xx.DataArrival += new DataArrivalEventHandler(xx_DataArrival);
        //xx.SendRequest(@"http://localhost:2080/examples/myservlets/servlet2","message=QQ&desttel=13910911925&mytel=13910081138&mypwd=263996211");
        xx.SendRequest(@"http://localhost","message=你好QQ&desttel=13910911925&mytel=13910081138&mypwd=263996211","GET",true);
        xx.SendRequest("http://localhost/quickstart/util/srcview.aspx?path=/quickstart/howto/samples/net/WebRequests/clientGETasync.src","post",false);
    }
private int i=0;
private void xx_DataArrival(object sender, DataArrivalEventArgs e)
    {
        Console.WriteLine( i++);
        Console.Write(e.RecievedData  );
        if (e.IsComplete)
            {
            Console.Write("[" + ((HttpRequestResponse) sender).Request.CookieContainer.GetCookieHeader(((HttpRequestResponse) sender).Response.ResponseUri).ToString() + "]");
            }
        }

//========================
//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()
    {
        BufferRead = new byte[BUFFER_SIZE];
        RequestData = new System.Text.StringBuilder("");
        Request = null;
        ResponseStream = 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)
    {
        mRecievedData = Data;
        mIsComplete = Complete;
    }
    public string RecievedData
    {
        get
        {
            return mRecievedData;
        }
    }
    public bool IsComplete
    {
        get
        {
            return mIsComplete;
        }
    }
}

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)
    {
        if (DataArrival != null)
        {
            DataArrival(this, e);
        }
    }
    private System.Text.Encoding mRequestEncoding = System.Text.Encoding.GetEncoding("gb2312");
    private bool mUseProxy = false;
    public bool UseProxy
    {
        get
        {
            return mUseProxy;
        }
        set
        {
            mUseProxy = value;
        }
    }

    private System.Net.WebProxy mWP = System.Net.WebProxy.GetDefaultProxy();
    public System.Net.WebProxy Proxy
    {
        get
        {
            return mWP;
        }
        set
        {
            mWP = value;
        }
    }

    private System.Net.HttpWebRequest mHWReq = null;
    public System.Net.HttpWebRequest Request
    {
        get
        {
            return mHWReq;
        }
    }
    
    private System.Net.HttpWebResponse mHWRsp = null;
    public System.Net.HttpWebResponse Response
    {
        get
        {
            return mHWRsp;
        }
    }
    public void SendRequest(string RequestURL,string RequestData,bool Async)
    {
        SendRequest(RequestURL,RequestData,"GET",Async);
    }
    public void SendRequest(string RequestURL,string RequestData,string RequestMethod,bool Async)
    {
        HttpWebRequest req;
        Request

上一个:一个用c#写的扫描asp源码漏洞的应用程序(续)
下一个:为了表示歉意,再贴一个 Socket 编的!让 CNet 加真酷!

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,