Write a download with C # under Dotnet

Source: Internet
Author: User
Tags dotnet get ip socket

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.