System.Net.WebClient client=new WebClient();
byte[] page=client.DownloadData("http://www.google.com");
string content=System.Text.Encoding.UTF8.GetString(page);
System.Net.WebClient client=new WebClient();
byte[] page=client.DownloadData("http://www.google.com");
string content=System.Text.Encoding.UTF8.GetString(page);
在dotnet下经常使用此类方法
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.1\r\nHost: " + hostName + "\r\nConnection: Close\r\n\r\n";
//创建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);
}
socket 方式以及模型:
不能看出跟程序的匹配,下载都是客户机的机制,解释出地址后,声明socket,发送一段报文,返回信息
报文string sendStr="GET "+"/tryst/default.asp"+" HTTP/1.1\r\nHost: " + hostName + "\r\nConnection: Close\r\n\r\n";
用get方式,得到网站里的某一个页面,如果想得到网页信息与webclient一样可以用正则表达式
http://liuxiaoyi666.cnblogs.com/archive/2006/06/02/415826.html
查看本文来源