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

为了表示歉意,再贴一个 Socket 编的!让 CNet 加真酷!

答案://调用示例
private void button1_Click(object sender, System.EventArgs e)
        {
            
            (new WindowsApplication1.HttpRequestResponse()).SendRequestAndGetResponse();
            (new WindowsApplication1.HttpRequestResponse()).SendRequestAndGetResponse();
            (new WindowsApplication1.HttpRequestResponse()).SendRequestAndGetResponse();
            (new WindowsApplication1.HttpRequestResponse()).SendRequestAndGetResponse();
            (new WindowsApplication1.HttpRequestResponse()).SendRequestAndGetResponse();
            (new WindowsApplication1.HttpRequestResponse()).SendRequestAndGetResponse();
            System.Windows.Forms.MessageBox.Show ("send end") ;

        }

//很久(去年)以前写的 Socket 级的 Class1.cs
using System;

    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
public class HttpRequestResponse
{
    private System.Text.StringBuilder SB = new System.Text.StringBuilder();
    private string sReq;
    private byte[] buffer = new byte[1024];
    private int index = 0;
    private static int Count =0;
    //public delegate SendRequestAndGetResponse
    public HttpRequestResponse()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    public void SendRequestAndGetResponse()
    {
            
            
        string sHost = "triatonweb";
        int iPort = 80;
        string sReqItem = "游客";
        sReqItem = "uid=" + System.Web.HttpUtility.UrlEncode(sReqItem,System.Text.Encoding.GetEncoding("GB2312"));
        sReq="userid=" + System.Web.HttpUtility.UrlEncode("游客",System.Text.Encoding.GetEncoding("GB2312"))
            + "&dest_tel=13910081138"
            + "&mytel=13910081138"
            + "&mypwd=asdada";
        string CGI = "dotnet/tree/index.asp";
        sReq = "POST /" + CGI + " HTTP/1.1\n"
            + "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\n"
            + "Accept-Language: zh-cn\n"
            + "Content-Type: application/x-www-form-urlencoded\n"
            + "Accept-Encoding: gzip, deflate\n"
            + "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)\n"
            + "Host: " + sHost + ":"  + System.Convert.ToString(iPort) +  "\n"
            + "Content-Length: " + System.Convert.ToString(sReqItem.Length) + "\n"
            + "Connection: Keep-Alive\n"
            + "Cache-Control: no-cache\n\n"
            + sReq;

        System.Net.Sockets.Socket Skt = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,System.Net.Sockets.SocketType.Stream,System.Net.Sockets.ProtocolType.Tcp);  
        index = ++Count;
        Skt.BeginConnect(new System.Net.IPEndPoint(System.Net.Dns.Resolve(sHost).AddressList[0], 80),
            new System.AsyncCallback (this.BeginConnectAsyncCallback),Skt);
        System.Console.WriteLine("Socket[" + index.ToString() + "] 开始连接: {0}",Skt.RemoteEndPoint.ToString());  


    }
    private void BeginConnectAsyncCallback(System.IAsyncResult IAR)
    {
        //            try
        //            {
        System.Net.Sockets.Socket Skt = (System.Net.Sockets.Socket) IAR.AsyncState;
        Skt.EndConnect(IAR);
            
        System.Console.WriteLine("Socket[" + index.ToString() + "] 已连接到: {0}",Skt.RemoteEndPoint.ToString());
        buffer =System.Text.Encoding.GetEncoding("GB2312").GetBytes(sReq) ;
        Skt.BeginSend(buffer,0,buffer.Length,System.Net.Sockets.SocketFlags.None,new System.AsyncCallback (this.BeginSendAsyncCallback) ,Skt);
        System.Console.WriteLine("Socket[" + index.ToString() + "] 开始发送: {1} Bytes 数据,到 IP: {0} ",Skt.RemoteEndPoint.ToString(),buffer.Length.ToString() );                            
        //            }
        //            catch (Exception e)
        //            {
        //             

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

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