用Socket下载HTML网页,为何文件没有下载完呀,高手给看看!代码是。。。
CSocket sock;
sock.Create();
sock.Connect(m_url,80);
CString str;
str="GET / HTTP/1.1\r\n";
str+="Accept: image//gif, image//x-xbitmap, image//jpeg, image//pjpeg, application//x-shockwave-flash, application//vnd.ms-excel, application//vnd.ms-powerpoint, application//msword, *//*\r\n";
str+="Accept-Language: zh-cn\r\n";
str+="Accept-Encoding: gzip, deflate\r\n";
str+="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)\r\n";
str+="Host:";
str+=m_url;
str+="\r\n";
str+="Connection: Keep-Alive\r\n\r\n";
sock.Send(str,str.GetLength(),0);
m_post=str;
CFile fp;
fp.Open("c:\\test.htm",CFile::modeCreate|CFile::modeWrite);
DWORD len=0;
char buf[4096];
memset(buf,0,4096);
len=sock.Receive(buf,4096);
fp.Write(buf,4096);
while(len>=4096)
{
memset(buf,0,4096);
len=sock.Receive(buf,4096);
fp.Write(buf,4096);
}
sock.Close();
fp.Close();
WSACleanup();