C++如何获取一个网页连接的网页源文件,windows API函数是否提供了接口(不用MFC) 20
1个回答
展开全部
这个要学socket编程了,我给你个简单的例子,获取百度音乐搜索“害怕”后得到的网页,当然因为临时弄出来的,所以可以运行但是发送的请求有点问题,所以百度服务器说我的请求内容没有找到,但是弄socket大概就是这样一个框架了,展示给你看下吧,要想真正的理解,去学socket吧
#include"winsock2.h"
#include"stdio.h"
#include"windows.h"
#include"conio.h"
#pragma comment(lib,"WS2_32.lib")
void main()
{
WSADATA data;
WORD w=MAKEWORD(2,0);
WSAStartup(w,&data);
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
char text6[500]="GET /m?f=ms&rf=idx&tn=baidump3&ct=134217728&lf=&rn=&word=%BA%A6%C5%C2&lm=0 HTTP/1.1\r\n"
"HOST:mp3.baidu.com\r\n"
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1\r\n"
"Accept: text/thml,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
"Accept-Language: zh-CN;zh;q=0.8\r\n"
"Accept-Encoding: gzip,deflate,sdch\r\n"
"Connection: keep-alive\r\n"
"Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3\r\n"
"\r\n\r\n";
sockaddr_in sa;
sa.sin_family=AF_INET;
sa.sin_port = htons(80);
sa.sin_addr.S_un.S_addr = inet_addr("220.181.5.39");
printf("连接服务器中。。。。。\n");
connect(sock,(sockaddr*)&sa,sizeof(sa));
send(sock,text6,sizeof(text6),0);
char text15[5000];
ZeroMemory(text15,sizeof(text15));
recv(sock,text15,sizeof(text15),0);
printf("%s\n",text15);
}
#include"winsock2.h"
#include"stdio.h"
#include"windows.h"
#include"conio.h"
#pragma comment(lib,"WS2_32.lib")
void main()
{
WSADATA data;
WORD w=MAKEWORD(2,0);
WSAStartup(w,&data);
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
char text6[500]="GET /m?f=ms&rf=idx&tn=baidump3&ct=134217728&lf=&rn=&word=%BA%A6%C5%C2&lm=0 HTTP/1.1\r\n"
"HOST:mp3.baidu.com\r\n"
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1\r\n"
"Accept: text/thml,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
"Accept-Language: zh-CN;zh;q=0.8\r\n"
"Accept-Encoding: gzip,deflate,sdch\r\n"
"Connection: keep-alive\r\n"
"Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3\r\n"
"\r\n\r\n";
sockaddr_in sa;
sa.sin_family=AF_INET;
sa.sin_port = htons(80);
sa.sin_addr.S_un.S_addr = inet_addr("220.181.5.39");
printf("连接服务器中。。。。。\n");
connect(sock,(sockaddr*)&sa,sizeof(sa));
send(sock,text6,sizeof(text6),0);
char text15[5000];
ZeroMemory(text15,sizeof(text15));
recv(sock,text15,sizeof(text15),0);
printf("%s\n",text15);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询