对指定域名获取网页内容
我想问下,下面的程序获取的html文件为什么用浏览器打开失真了?获取数据哪里有问题吗?
std::cout<<strlen(buf)<<std::endl
而且这一句老是输出4104。。。
谢谢指教。void main(int argc, char* argv[])
{
WORD version=MAKEWORD(1,1);
WSADATA wsaData;
int err=WSAStartup(version,&wsaData);
if ( err != 0 ) {
return;
}
if (LOBYTE(wsaData.wVersion)!=1
|| HIBYTE(wsaData.wVersion)!=1)
{
WSACleanup();
return;
}
SOCKET sock=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN sockadrr;
char *host_server="www.163.com";
hostent *pHost=gethostbyname(host_server);
LPCSTR psz=inet_ntoa (*(in_addr*)pHost->h_addr_list);
sockadrr.sin_addr.S_un.S_addr=inet_addr(psz);
sockadrr.sin_family=AF_INET;
sockadrr.sin_port=htons(80);
char *Command="GET /index.html HTTP/1.1 \r\nHost: www.163.com\r\nAccept: image/gif \r\n\r\n";
int len=sizeof(SOCKADDR);
connect(sock,(SOCKADDR*)&sockadrr,len);
sendto(sock,Command,strlen(Command)+1,0,(SOCKADDR*)&sockadrr,len);
std::ofstream ous("C:\\Documents and Settings\\Administrator\\桌面\\163com.html",std::ios::binary);
char buf;
while(recvfrom(sock,buf,4096,0,(SOCKADDR*)&sockadrr,&len)>0)
{
std::cout<<strlen(buf)<<std::endl;
if(strlen(buf)<4096)buf=0;
ous<<buf;
}
ous.close();
int wsaerr=WSAGetLastError();
std::cout<<wsaerr<<std::endl;
}
void main(int argc, char* argv[])
{
WORD version = MAKEWORD(1,1);
WSADATA wsaData;
int err = WSAStartup(version,&wsaData);
if(err != 0)
return;
if(LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
{
WSACleanup();
return;
}
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN sockadrr;
char *host_server = "www.163.com";
hostent *pHost = gethostbyname(host_server);
LPCSTR psz = inet_ntoa (*(in_addr*)pHost->h_addr_list);
sockadrr.sin_addr.S_un.S_addr = inet_addr(psz);
sockadrr.sin_family = AF_INET;
sockadrr.sin_port = htons(80);
char *Command = "GET /index.html HTTP/1.1 \r\nHost: www.163.com\r\nAccept: image/gif \r\n\r\n";
int len = sizeof(SOCKADDR);
connect(sock, (SOCKADDR*)&sockadrr, len);
send(sock, Command, strlen(Command), 0);
std::ofstream ous("163com.html", std::ios::binary);
char buf;
/*
std::cout<<strlen(buf)<<std::endl
而且这一句老是输出4104。。。
为什么啊?
用memset 一下就OK!
*/
memset(buf, 0, 4096);
while(recv(sock, buf, 4096, 0)>0)
{
std::cout << strlen(buf) << std::endl;
if(strlen(buf) < 4096)
buf = 0;
ous << buf;
memset(buf, 0, 4096);
}
ous.close();
int wsaerr = WSAGetLastError();
std::cout << wsaerr << std::endl;
}
看一看吧!已经没有问题了!HTTP协议是以TCP协议为基础的,记得TCP,不能用sendto
页:
[1]