I
I
Igor2019-07-17 15:51:30
Delphi
Igor, 2019-07-17 15:51:30

How to convert text to normal encoding in delphi?

Guys, it's not funny anymore.
3 days I've been having a question.
I am using the THttpClient class.
Set the correct headers.

AHeaders[0].Create('Content-Type', 'text/html; charset=UTF-8');
   AHeaders[1].Create('Accept', 'application/json, text/plain; q=0.9, text/html;q=0.8');
   AHeaders[2].Create('Accept-Language', 'ru-RU,ru');
   AHeaders[3].Create('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36');
   AHeaders[4].Create('Accept-Charset', 'utf-8; q=0.5');
   AHeaders[5].Create('Accept-Encoding', 'gzip, deflate, br');

5d2f1845f2058611736100.png
Here is the full code in the response :
function TMKCardLoader.LoadContent(const url: string): string;
const
 ERROR_LIMIT = 5;
 EXCEPT_SLEEP = 10000;
var
  http: THttpClient;
  uri: TURI;
  ResponseStream: TStringStream;
  Sl: TStringList;
  Response: IHTTPResponse;

  ErrorCounter: integer;
  IsExcept: boolean;

  AHeaders: TNetHeaders;
label
  Reload;
begin
 try
   Result := '';
   uri := TURI.Create(FBaseUrl);
   uri.Path := url;
   ResponseStream := TStringStream.Create('', TEncoding.Default);
   http := THTTPClient.Create;
   http.ConnectionTimeout := 12000;
   http.AllowCookies := true;
   http.CookieManager := TCookieManager.Create;
   http.HandleRedirects := true;

   SetLength(AHeaders, 6);

   AHeaders[0].Create('Content-Type', 'text/html; charset=UTF-8');
   AHeaders[1].Create('Accept', 'application/json, text/plain; q=0.9, text/html;q=0.8');
   AHeaders[2].Create('Accept-Language', 'ru-RU,ru');
   AHeaders[3].Create('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36');
   AHeaders[4].Create('Accept-Charset', 'utf-8; q=0.5');
   AHeaders[5].Create('Accept-Encoding', 'gzip, deflate, br');

   ErrorCounter := 0;
   Reload:
   IsExcept:= false;
   try
     Response := http.Get(uri.ToString, ResponseStream, AHeaders);
     result := ResponseStream.DataString;
   except
      on E: Exception do
      begin
        OutputDebugString(Pwidechar(E.Message));
        Inc(ErrorCounter);
        IsExcept := true;
      end;
    end;

    if IsExcept then
      if ErrorCounter >= ERROR_LIMIT then
        exit
      else
      begin
        Sleep(EXCEPT_SLEEP);
        goto Reload;
      end;
 finally
   ResponseStream.Free;
   http.Free;
 end;
end;

If you have paid attention to
ResponseStream := TStringStream.Create('', TEncoding.Default);

Here I indicated utf-8
And yes, the text is read normally, but it does not work stably, even very unstable.
try
     Response := http.Get(uri.ToString, ResponseStream, AHeaders);
     result := ResponseStream.DataString; // тут всё разрушается если в TStringStream.Create('', TEncoding.utf8);
   except
      on E: Exception do
      begin
        OutputDebugString(Pwidechar(E.Message));
        Inc(ErrorCounter);
        IsExcept := true;
      end;
    end;

Exception 'No mapping for the Unicode character exists in the target multi-byte code page'
Summary:
An exception is thrown if TStringStream.Create('', TEncoding.utf8);
If TStringStream.Create('', TEncoding.Default); // no exception, but crocodile

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor, 2019-07-17
@IgorPI

Colleagues!
UTF8ToString is the solution!
Thank you!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question