网页源文件获取乱码问题!

sharp0309 2011-01-10 02:40:26
第三次提问了!环境VS2005 或者 vc6
http://news.sohu.com/20101230/n278604307.shtml

谁能给个代码将这个链接的网页源文件获取到。(不能乱码)
希望牛人们帮我解决这个问题。
同时希望是经过测试的代码。
...全文
199 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
vansbluge 2011-01-11
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 beyond0824 的回复:]

符合你的要求,可以直接复制
C/C++ code

#include <windows.h>
#include "Wininet.h"
#include <iostream>
#include <string>

#pragma comment(lib, "Wininet.lib")

using namespace std;

enum Html_Type
{
CODE_UTF8,……
[/Quote]

这个是自己指定编码啊..
sharp0309 2011-01-11
  • 打赏
  • 举报
回复
to beyond0824
你的代码测试过吗?还是不行的
需要gzip来解压的
covsno 2011-01-11
  • 打赏
  • 举报
回复
有个P乱码
就GZIP解压出来就可以了

不信你自己下载
用UE把HTTP头去掉,就可以用winrar打开了
江南烟雨梦 2011-01-10
  • 打赏
  • 举报
回复
符合你的要求,可以直接复制

#include <windows.h>
#include "Wininet.h"
#include <iostream>
#include <string>

#pragma comment(lib, "Wininet.lib")

using namespace std;

enum Html_Type
{
CODE_UTF8, //UTF8编码的网站
CODE_GB2312, //GB2312编码的网站
};

string DownHtmlContent(const string &strUrl, Html_Type type);

int main()
{
string strHtml("");
string strUrl = "http://news.sohu.com/20101230/n278604307.shtml";

strHtml = DownHtmlContent(strUrl, CODE_GB2312);
cout << strHtml.c_str() << endl;
}


string DownHtmlContent(const string &strUrl, Html_Type type)
{
string strContent;
string strRooK = "RookIE/1.0";
string strUrlTmp = strUrl;

HINTERNET hSession = InternetOpen("RookIE/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession != NULL)
{
HINTERNET handle2 = InternetOpenUrl(hSession, strUrl.c_str(), NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
if (handle2 != NULL)
{
char *Temp = new char[1024*1024];
ZeroMemory(Temp, 1024*1024);
DWORD Number = 1;
DWORD Total = 0;
BOOL bRes = FALSE;
while ( Number > 0 )
{
if( InternetReadFile(handle2, Temp+Total, 2048, &Number) )
{
Total += Number;
}
}
if ( type == CODE_GB2312 )
{
strContent = Temp;
}
else if ( type == CODE_UTF8 )
{
wchar_t *pUnicode = new wchar_t[1024*1024];
char *pAnsi = new char[1024*1024];
ZeroMemory( pAnsi, 1024*1024 );
ZeroMemory( pUnicode, 1024*1024*2 );
if( MultiByteToWideChar( CP_UTF8, 0, Temp, Total, pUnicode, 1024*1024 ) != 0 )
{
WideCharToMultiByte( CP_ACP, 0, pUnicode, wcslen( pUnicode ), pAnsi, 1024*1024, "", NULL );
strContent = pAnsi;
}
delete []pAnsi;
delete []pUnicode;
}
delete []Temp;
InternetCloseHandle(handle2);
handle2 = NULL;
}
InternetCloseHandle(hSession);
hSession = NULL;
}

return strContent;
}
江南烟雨梦 2011-01-10
  • 打赏
  • 举报
回复
我试了一下,没有乱码,完全正常!
yeah2000 2011-01-10
  • 打赏
  • 举报
回复
你查找一下 gbk 转 gb2312 的算法或者函数,在你获得页面内容之后,转换一下文字编码即可
jwybobo2007 2011-01-10
  • 打赏
  • 举报
回复
用gzip反编码
MervynChina 2011-01-10
  • 打赏
  • 举报
回复
要经过一定的编码转换
sharp0309 2011-01-10
  • 打赏
  • 举报
回复
希望牛人们给代码哈!!经过测试!
qiri07 2011-01-10
  • 打赏
  • 举报
回复
需要根据网友获取到的编码charset进行编码转换
sharp0309 2011-01-10
  • 打赏
  • 举报
回复
希望牛人们不要用控件哈

18,356

社区成员

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

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