请求http接口的时候参数值是中文乱码:
http接口一般都是由java,php以及C#开发而成的,乱码的原因也是由于编码的问题,一般传递数据的都是utf8,然后传递的时候都会urlEcode
那么delphi如何解决这个问题呢?
请看下面的实例,实例中的“哈哈”二字就是中文:
procedure TClientForm.Button77Click(Sender: TObject);
var paramStream: TIdMultiPartFormDataStream;
vResponse: TStringStream;
filename, vres: string;
vList: TStringList;
jsonstr,signStr,timestamp,sign: string;
begin
//生成sign
timestamp := FormatFloat(‘#‘, DateUtils.MilliSecondsBetween(Now, StrToDateTime(‘1970-1-1‘)) - 8 * 60 * 60);
signStr:=‘schNo=1000066&schRecords=‘+httpencode(UTF8Encode(‘哈哈‘));
sign:=MD5.StrMD5(signStr);
paramStream := TIdMultiPartFormDataStream.Create;
vResponse := TStringStream.Create(‘‘);
vList := TStringList.Create;
// paramStream.AddFile(‘FileInputStream‘, ‘D:\1.xlsx‘, GetMIMETypeFromFile(‘D:\1.xlsx‘));
//paramStream.AddFormField(‘fileName‘, UTF8Encode(‘1.xlsx‘));
paramStream.AddFormField(‘signature‘, sign);
paramStream.AddFormField(‘schRecords‘, UTF8Encode(‘哈哈‘));
paramStream.AddFormField(‘N‘, ‘1‘);
IdHTTP1.Post(‘http连接‘, paramStream, vResponse);
vres := (vResponse.DataString); //Utf8ToAnsi
Memo1.Text := Utf8ToAnsi(vres);
// ShowMessage(vres);
end;