C语言实现网页源代码获取

#include<iostream>
#include <WINSOCK2.H>
#pragma comment(lib,"ws2_32.lib")
#define LEN 1024	//接收数据的大小
using namespace std;


int main()
{
	//加载套接字库
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;

	wVersionRequested = MAKEWORD( 1, 1 );    //初始化Socket动态连接库,请求1.1版本的winsocket库

	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 ) {
		return 0;
	}

	if ( LOBYTE( wsaData.wVersion ) != 1 ||   //判断请求的winsocket是不是1.1的版本
		HIBYTE( wsaData.wVersion ) != 1 ) {
			WSACleanup( );			//清盘
			return 0;					//终止对winsocket使用
	}
	//WSADATA ws;
	//WSAStartup(MAKEWORD(2,2),&ws);//
	char http[60] = "www.google.com";			//访问谷歌网页
	SOCKET sock = socket(AF_INET,SOCK_STREAM,0);//建立socket
	if (sock == INVALID_SOCKET)
	{
		cout<<"建立访问socket套接字失败!"<<endl;
		return 0;
	}
	sockaddr_in hostadd;
	hostent* host = gethostbyname(http);//取得主机的IP地址
	if(host==NULL)
	{
		cout<<"主机处于没有联网状态;"<<endl;
		return 0;
	}
	cout<<"主机处于联网状态,现在可以进行通信!"<<endl;
	memcpy(&hostadd,host->h_addr,sizeof(hostadd));//将返回的IP信息Copy到地址结构
	hostadd.sin_family = AF_INET;
	hostadd.sin_port = htons(80);


	char buf[LEN]="GET / HTTP/1.1\r\nHost: ";//构造Http请求数据包
	strcat(buf,inet_ntoa(hostadd.sin_addr));
	strcat(buf," \r\nContent-Length: 10\r\n\r\n");
	strcat(buf,"Connection:close");


	int time = 1000;	//超时时间
	setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(char*)&time,sizeof(time));


	if (connect(sock,(sockaddr*)&hostadd,sizeof(hostadd)) == SOCKET_ERROR)//连接请求
	{
		cout<<"与网页建立连接失败!"<<endl;
		return 0;
	}
	if (SOCKET_ERROR == send(sock,buf,strlen(buf)+1,0))//发送构造好的Http请求包
	{
		cout<<"发送数据包失败!"<<endl;
		return 0;
	}
	memset(buf,0,LEN);
	recv(sock,buf,LEN,0);				//接收返回的数据
	cout<<"从网页中获取的数据为:"<<buf;
	closesocket(sock);
	WSACleanup();
	return 0;
}

  

#include<iostream>
#include <WINSOCK2.H>  //判断是否联网
#pragma comment(lib,"ws2_32.lib")
#define LEN 1024	//接收数据的大小
using namespace std;


int main()
{
	//加载套接字库
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;

	wVersionRequested = MAKEWORD( 1, 1 );    //初始化Socket动态连接库,请求1.1版本的winsocket库

	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 ) {
		return 0;
	}

	if ( LOBYTE( wsaData.wVersion ) != 1 ||   //判断请求的winsocket是不是1.1的版本
		HIBYTE( wsaData.wVersion ) != 1 ) {
			WSACleanup( );			//清盘
			return 0;					//终止对winsocket使用
	}
	//WSADATA ws;
	//WSAStartup(MAKEWORD(2,2),&ws);//

	char http[60] = "www.sina.com";
//	char http[60] = "iframe.ip138.com/ic.asp";			//访问谷歌网页
	SOCKET sock = socket(AF_INET,SOCK_STREAM,0);//建立socket
	if (sock == INVALID_SOCKET)
	{
		cout<<"建立访问socket套接字失败!"<<endl;
		return 0;
	}
	sockaddr_in hostadd;
	hostent* host = gethostbyname(http);//取得主机的IP地址
	if(host==NULL)
	{
		cout<<"主机处于没有联网状态;"<<endl;
		return 0;
	}
	cout<<"主机处于联网状态,现在可以进行通信!"<<endl;
	memcpy(&hostadd,host->h_addr,sizeof(hostadd));//将返回的IP信息Copy到地址结构
	hostadd.sin_family = AF_INET;
	hostadd.sin_port = htons(80);


	char buf[LEN]="GET / HTTP/1.1\r\nHost: ";//构造Http请求数据包
	strcat(buf,inet_ntoa(hostadd.sin_addr));
	strcat(buf," \r\nContent-Length: 10\r\n\r\n");
	strcat(buf,"Connection:close");


	int time = 1000;	//超时时间
	setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(char*)&time,sizeof(time));


	if (connect(sock,(sockaddr*)&hostadd,sizeof(hostadd)) == SOCKET_ERROR)//连接请求
	{
		cout<<"与网页建立连接失败!"<<endl;
		return 0;
	}
	if (SOCKET_ERROR == send(sock,buf,strlen(buf)+1,0))//发送构造好的Http请求包
	{
		cout<<"发送数据包失败!"<<endl;
		return 0;
	}
	memset(buf,0,LEN);
	recv(sock,buf,LEN,0);				//接收返回的数据
	cout<<"从网页中获取的数据为:"<<buf;
	closesocket(sock);
	WSACleanup();
	getchar();
	getchar();
	getchar();
	getchar();getchar();
	getchar();getchar();
	getchar();
	return 0;
}

  

转载于:https://www.cnblogs.com/yan1345/p/3443972.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>