为了表示歉意,再贴一个 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)
// {
// Console.WriteLine(e.ToString());
// }
}
private void BeginSendAsyncCallback(System.IAsyncResult IAR)
{
// try
// {
System.Net.Sockets.Socket Skt = (System.Net.Sockets.Socket) IAR.AsyncState;
Skt.EndSend(IAR);
System.Console.WriteLine("Socket[" + index.ToString() + "] 发送完毕");
//byte[] buffer = new byte[1024];
Skt.BeginReceive(buffer,0,buffer.Length,System.Net.Sockets.SocketFlags.None,new System.AsyncCallback(this.BeginRecieveAsyncCallback),Skt);
System.Console.WriteLine("Socket[" + index.ToString() + "] 开始接收数据,从 IP: {0}",Skt.RemoteEndPoint.ToString());
// }
// catch (Exception e)
// {
// Console.WriteLine(e.ToString());
// }
}
private void BeginRecieveAsyncCallback(System.IAsyncResult IAR)
{
// try
// {
System.Net.Sockets.Socket Skt = (System.Net.Sockets.Socket) IAR.AsyncState;
int i =Skt.EndReceive(IAR);
if (i>0)
{
//buffer.Length = i;
SB.Append("" + System.Text.Encoding.GetEncoding("GB2312").GetString(buffer,0,i));
Skt.BeginReceive(buffer,0,buffer.Length,System.Net.Sockets.SocketFlags.None,new System.AsyncCallback(this.BeginRecieveAsyncCallback),Skt);
System.Console.WriteLine("Socket[" + index.ToString() + "] 已接收 {1} 字节数据,从 IP: {0}",Skt.RemoteEndPoint.ToString(),buffer.Length.ToString() );
Console.WriteLine(System.Text.Encoding.GetEncoding("GB2312").GetString(buffer,0,i) );
}
else
{
Skt.Close();
Skt = null;
Console.WriteLine(SB.ToString());
}
//Skt.BeginReceive
// }
// catch (Exception e)
// {
// Console.WriteLine(e.ToString());
// }
}
}
Tags:
作者:佚名评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论