2009-12-04 64 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