string hostName="www.nnn.net"; label1.Text="xxx"; int port=80; IPHostEntry ipInfo=Dns.GetHostByName(hostName); //取得IPAddress[] IPAddress[] ipAddr=ipInfo.AddressList; //得到ip IPAddress ip=ipAddr[0]; IPEndPoint hostEP=new IPEndPoint(ip,port); Socket socket=new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp); socket.Connect(hostEP); string sendStr="GET "+"/tryst/default.asp"+" HTTP/1.1rnHost: " + hostName + "rnConnection: Closernrn"; //创建bytes字节数组以转换发送串 byte[] bytesSendStr=new byte[1024]; //将发送内容字符串转换成字节byte数组 bytesSendStr=Encoding.ASCII.GetBytes(sendStr); socket.Send(bytesSendStr,bytesSendStr.Length,0); //声明接收返回内容的字符串 string recvStr=""; //声明字节数组,一次接收数据的长度为1024字节 byte[] recvBytes=new byte[1024]; //返回实际接收内容的字节数 int bytes=0; //循环读取,直到接收完所有数据 while(true) { bytes=socket.Receive(recvBytes,recvBytes.Length,0); //读取完成后退出循环 if(bytes<=0) break; //将读取的字节数转换为字符串 recvStr+=Encoding.ASCII.GetString(recvBytes,0,bytes); }
|