4 楼
SwiftHorse
2008-03-24
到今天有了新的进展,但也碰到了新的问题。对于日文(Shift-JIS)编码可以正常通过IDHTTP取到正确的源文件,但对于日文EUC編碼去却以下程式不能得到正确结果,不解。贴出我的代码,可以供需要取Shift-JIS编码的朋友参考。
var
IframeUrl: string;
ret:WideString;//TStringStream;
SourceHtmlCode: WideString;
begin
IframeUrl := 'http://ctplp.blog15.fc2.com/blog-entry-33.html';//这个网站是EUC-JS编码
//IframeUrl := 'http://www.nhk.or.jp/furusato/koremade/koremade_ibaraki.html';//这个网站是SHIFT-JS编码
IdHTTP.HandleRedirects:=true;
IdHTTP.Request.ContentType:= 'application/x-www-form-urlencoded';
IdHTTP.Request.UserAgent:= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)';
IdHTTP.Request.SetHeaders;
try
SourceHtmlCode := StringToWideString(IdHTTP.Get(IframeUrl),51932);//日文EUC編碼是51932 日文(Shift-JIS)是932
Memo1.Lines.Text := SourceHtmlCode;
except
ShowMessage('未找到HTTP服务器');
end;
end;
function StringToWideString(const S: string; CodePage: Word): WideString;
var
InputLength, OutputLength: Integer;
begin
InputLength := Length(S);
OutputLength := MultiByteToWideChar(CodePage, 0, PChar(S), InputLength, nil, 0);
SetLength(Result, OutputLength);
MultiByteToWideChar(CodePage, 0, PChar(S), InputLength, PWideChar(Result), OutputLength);
end;