System.Net.WebClient client=new WebClient();
byte[] page=client.DownloadData(http://www.bianceng.cn);
string content=System.Text.Encoding.UTF8.GetString(page);
This type of method is often used under dotnet:
string Hostname= "Www.nnn.net";
Label1. text= "XXX";
int port=80;
Iphostentry Ipinfo=dns.gethostbyname (hostName);
//Get ipaddress[]
ipaddress[] ipaddr=ipinfo.addresslist
//Get 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";
//Create bytes byte array to convert send string
byte[] bytessendstr=new byte[1024];
//Convert send content string to byte byte array
bytessendstr= Encoding.ASCII.GetBytes (SENDSTR);
Socket. Send (bytessendstr,bytessendstr.length,0);
//declares a string recvstr= to receive the returned content
string "";
///Declaration byte array, 1024 bytes long for receiving data
Byte[] recvbytes=new byte[1024];
// Returns the number of bytes of the actual received content
int bytes=0;
//loop read until all data is received
while (true)
{
Bytes=socket. Receive (recvbytes,recvbytes.length,0);
//exit loop after read completion
if (bytes<=0)
Break
//Converts the number of bytes read to a string
recvstr+=encoding.ascii.getstring (recvbytes,0,bytes);
}
Socket mode and model:
Can not see the match with the program, the download is the mechanism of the client, explain the address, declare the socket, send a message, return information.
Message string sendstr= "get" + "/tryst/default.asp" + "Http/1.1\r\nhost:" + hostName + "\r\nconnection:close\r\n\r\n";
Get the way to a site in a certain page, if you want to page information and WebClient can use regular expressions.