* * *

Author Topic: idhttp.get can not get datas in linux.  (Read 272 times)

wylton

  • New member
  • *
  • Posts: 40
idhttp.get can not get datas in linux.
« on: April 24, 2018, 03:28:30 pm »
 IdHTTP1.Get("http://hq.sinajs.cn/list=sz000933,sh600795", IndyTextEncoding('utf-8'))

it work on windows, but not on linux.

Remy Lebeau

  • Sr. Member
  • ****
  • Posts: 399
    • Lebeau Software
Re: idhttp.get can not get datas in linux.
« Reply #1 on: April 26, 2018, 10:48:32 pm »
In what way exactly does it not work?  Please be more specific.

When I request that URL, the response's 'Content-Type' header reports 'charset=GBK'.  TIdHTTP will thus attempt to decode the response body data from GBK to UTF-16, and then convert that result to UTF-8.  On Linux, those conversions are done using the libiconv library.  On Windows, those conversions are done using the Win32 API MultiByteToWideChar() and WideCharToMultiByte() functions using codepages 936 and 65001, respectively.

I would suggest downloading the server's data as raw bytes instead of as a string, and then make sure the bytes really are GBK encoded, and then manually check whether it is the GBK->UTF16 or UTF16->UTF8 conversion that is failing, eg:

Code: [Select]
var
  Strm: TMemoryStream;
  Bytes: TIdBytes;
  sTemp: TIdUnicodeString;
  sResult: string;
begin
  Strm := TMemoryStream.Create;
  try
    IdHTTP1.Get("http://hq.sinajs.cn/list=sz000933,sh600795", Strm);
    Strm.Position := 0;
    ReadTIdBytesFromStream(Strm, Bytes, -1);
  finally
    Strm.Free;
  end;
  // verify Bytes are GBK encoded...
  sTemp := IndyTextEncoding('GBK').GetString(Bytes);
  // verify sTemp is correct...
  Bytes := IndyTextEncoding('utf-8').GetBytes(sTemp);
  // verify Bytes is correct...
  SetString(sResult, PAnsiChar(Bytes), Length(Bytes));
end;

Also, just curious, why are you using IndyTextEncoding('utf-8') instead of using IndyTextEncoding_UTF8()?  It doesn't really matter in this case, as the former will just call the latter for you.
« Last Edit: April 26, 2018, 11:17:40 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus