delphi TIdHTTP Post乱码问题和调用百度API获取token

delphi TIdHTTP Post乱码问题

这里主要说的是中文乱码的问题

1. 发过去的是乱码如下处理, 服务器采用的是UFT-8编码的情况下

uses HTTPApp;

sPost := HTTPEncode(UTF8Encode('中文处理'));

2. 返回的是乱码如下处理

ResponseStream:=TStringstream.Create('',TEncoding.UTF8);

idhttp.Post(ConnectUrl,sends,ResponseStream);

 

delphi调用百度API获取token

procedure TForm1.btn_getTokenClick(Sender: TObject);
var
sendData: string;
url: string;
ss: TStringStream;
jsonData: isuperobject;
begin
url := 'https://aip.baidubce.com/oauth/2.0/token';
sendData := 'grant_type=client_credentials&client_id=' + Self.cxTextEdit_clientID.Text + '&client_secret=' + Self.cxTextEdit_clientSecret.Text;

try
ss := TStringStream.Create('', TEncoding.UTF8);
ss.WriteString(sendData);
self.NetHTTPClient1.Accept := 'text/html, application/xhtml+xml, */*';
self.NetHTTPClient1.ContentType := 'application/x-www-form-urlencoded'; //application/json
self.NetHTTPClient1.AcceptEncoding := 'UTF-8';
ss.Position := 0;

jsonData := SO(NetHTTPClient1.Post(url, ss).ContentAsString(tencoding.UTF8));

if Assigned(jsonData['access_token']) then
begin
cxMemo1.Clear;
cxMemo1.Lines.Add(jsonData['access_token'].AsString);
access_token := jsonData['access_token'].AsString;
end;

finally
ss.Free;
end;

end;

 

THE END