使用Socket获取网页源代码的代码,一般需要以下几个步骤:
- 解析主机名和IP地址:
使用Dns类解析主机名(如www.baidu.com)对应的IP地址。代码如下:
IPHostEntry hostEntry = Dns.GetHostEntry("www.baidu.com");
IPAddress ipAddress = hostEntry.AddressList[0];
- 建立Socket连接:
使用IPAddress和端口号建立Socket连接。代码如下:
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 80);
Socket socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(remoteEP);
- 发送HTTP请求:
发送HTTP请求,包括请求首部和消息体(如果有),并接收服务器的响应消息。代码如下:
string request = "GET / HTTP/1.1\r\nHost: www.baidu.com\r\n\r\n";
byte[] requestBytes = Encoding.ASCII.GetBytes(request);
socket.Send(requestBytes);
- 接收响应消息:
接收服务器的响应消息(包括响应首部和消息体),并将其转换成字符串格式。代码如下:
byte[] recvBytes = new byte[1024];
int bytes;
string response = "";
do {
bytes = socket.Receive(recvBytes, recvBytes.Length, 0);
response += Encoding.ASCII.GetString(recvBytes, 0, bytes);
} while (bytes > 0);
示例1:
获取百度首页的源代码:
IPAddress ipAddress = Dns.GetHostEntry("www.baidu.com").AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 80);
Socket socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(remoteEP);
string request = "GET / HTTP/1.1\r\nHost: www.baidu.com\r\n\r\n";
byte[] requestBytes = Encoding.ASCII.GetBytes(request);
socket.Send(requestBytes);
byte[] recvBytes = new byte[1024];
int bytes;
string response = "";
do {
bytes = socket.Receive(recvBytes, recvBytes.Length, 0);
response += Encoding.ASCII.GetString(recvBytes, 0, bytes);
} while (bytes > 0);
Console.WriteLine(response);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
示例2:
获取GitHub首页的源代码:
IPAddress ipAddress = Dns.GetHostEntry("github.com").AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 80);
Socket socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(remoteEP);
string request = "GET / HTTP/1.1\r\nHost: github.com\r\n\r\n";
byte[] requestBytes = Encoding.ASCII.GetBytes(request);
socket.Send(requestBytes);
byte[] recvBytes = new byte[1024];
int bytes;
string response = "";
do {
bytes = socket.Receive(recvBytes, recvBytes.Length, 0);
response += Encoding.ASCII.GetString(recvBytes, 0, bytes);
} while (bytes > 0);
Console.WriteLine(response);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#中使用Socket获取网页源代码的代码 - Python技术站