2009-12-04 100 views
2

我有德尔福2009年/ 2010阅读网页/ Unicode的

这个功能它返回的垃圾,现在如果我改变焦炭,PChar类型类型ANSIChar一致,Pansichar它返回的文本,但所有外国Unicode文本是垃圾。它把我的香蕉 我一直在尝试2天所有类的东西,现在 我想我understoff这个unicode废话,但我想我不 请帮助 感谢 菲利普Watel

function GetInetFileAsString(const fileURL: string): string; 
const 
    C_BufferSize = 1024; 
var 
    sAppName: string; 
    hSession, 
    hURL: HInternet; 

    Buffer: array[0..C_BufferSize] of Char; 
    BufferLen: DWORD; 

    strPageContent: string; 
    strTemp: string; 

begin 
    Result := ''; 
    sAppName := ExtractFileName(Application.ExeName); 
    hSession := InternetOpen(PChar(sAppName), INTERNET_OPEN_TYPE_PRECONFIG, nil, 
    nil, 0); 
    try 
    hURL := InternetOpenURL(hSession, PChar(fileURL), nil, 0, 0, 0); 
    try 
     strPageContent := ''; 
     repeat 
     InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen); 
     SetString(strTemp, PChar(@buffer), BufferLen div SizeOf(Char)); 
     strPageContent := strPageContent + strTemp; 
     until BufferLen = 0; 
     Result := strPageContent; 
    finally 
     InternetCloseHandle(hURL) 
    end 
    finally 
    InternetCloseHandle(hSession) 
    end 
end; 

回答

0

我首先想到的是到正确的AcceptEncoding /字符集头添加到请求:

例如:

接收字符集:ISO-8859-1,utf-8; q = 0.7,*; q = 0.7

4

在2009年的Delphi开始,StringUnicodeString的别名,它保存UTF-16的数据。另一方面,HTML页面通常使用多字节Ansi编码进行编码(现在通常是UTF-8,但并非总是如此)。您的当前代码仅在HTML编码为UTF-16时才有效,这非常少见。您不应直接将原始HTML字节读取到UnicodeString中。您需要先将全部数据下载到TBytes,RawByteString,或您选择的其他合适的字节容器中,然后根据HTTP“Content-Type中指定的字符集执行Ansi-> Unicode转换“响应标题。您可以使用Accept-charset请求头来告诉服务器您希望将数据发送到哪个字符集,并且如果服务器无法使用该字符集,那么它应该发送406 Not Acceptable响应(尽管它可能仍然会发送成功响应不可接受的字符集,如果它选择忽略您的请求标题,所以你应该说明这一点)。

尝试这样:

function GetInetFileAsString(const fileURL: string): string; 
const 
    C_BufferSize = 1024; 
var 
    sAppName: string; 
    hSession, hURL: HInternet; 
    Buffer: array of Byte; 
    BufferLen: DWORD; 
    strHeader: String; 
    strPageContent: TStringStream; 
begin 
    Result := ''; 
    SetLength(Buffer, C_BufferSize); 
    sAppName := ExtractFileName(Application.ExeName); 
    hSession := InternetOpen(PChar(sAppName), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0); 
    try 
    strHeader := 'Accept-Charset: utf-8'#13#10; 
    hURL := InternetOpenURL(hSession, PChar(fileURL), PChar(strHeader), Length(strHeader), 0, 0); 
    try 
     strPageContent := TStringStream.Create('', TEncoding.UTF8); 
     try 
     repeat 
      if not InternetReadFile(hURL, PByte(Buffer), Length(Buffer), BufferLen) then 
      Exit; 
      if BufferLen = 0 then 
      Break; 
      strPageContent.WriteBuffer(PByte(Buffer)^, BufferLen); 
     until False; 
     Result := strPageContent.DataString; 
     // or, use HttpQueryInfo(HTTP_QUERY_CONTENT_TYPE) to get 
     // the Content-Type header, parse out its "charset" attribute, 
     // and convert strPageContent.Memory to UTF-16 accordingly... 
     finally 
     strPageContent.Free; 
     end; 
    finally 
     InternetCloseHandle(hURL); 
    end 
    finally 
    InternetCloseHandle(hSession); 
    end; 
end; 
+0

我使用的代码成功,但我改变缓冲区动态数组声明缓冲:在tarray ;然后SetLength(缓冲区,C_BufferSize)。另外,我在每次调用InternetReadFile之后,在循环之前将var整型参数设置为零,并使用bufferLen递增。让我知道下载文件的大小。 – MarkAurelius 2015-10-27 01:56:48