用Socket下载HTML网页,为何文件没有下载完呀,高手给看看!代码是。。。

wang_bluebird 2005-03-22 10:39:16
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();
...全文
80 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tomjun 2005-03-22
  • 打赏
  • 举报
回复
//不能保证每次都能收到4096个字节
//但一旦收到的量为0,就表示接收完毕了
while(len != 0)
{

memset(buf,0,4096);
len=sock.Receive(buf,4096);
fp.Write(buf,4096);
}
wang_bluebird 2005-03-22
  • 打赏
  • 举报
回复
后面不是有循环继续下载吗?
如何修改呀???
nuaawenlin 2005-03-22
  • 打赏
  • 举报
回复
len=sock.Receive(buf,4096);
你只接收了4096个字节,
good_0525 2005-03-22
  • 打赏
  • 举报
回复
下载的是HTML吗?怎么这么大(4G多)!

18,363

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧