Answer the question
In order to leave comments, you need to log in
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');
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;
ResponseStream := TStringStream.Create('', TEncoding.Default);
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;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question