DELPHI XE7 DATASNAP Server接收到的中文乱码

FOREST169 2019-04-17 11:18:13
服务端DELPHI XE7:
procedure TServerContainer2.DSHTTPService1HTTPTrace(Sender: TObject;
AContext: TDSHTTPContext; ARequest: TDSHTTPRequest;
AResponse: TDSHTTPResponse);
VAR
AP :TSTRINGS;
SIP, SPORT :ANSISTRING;
S1, CH, RD :STRING;
LL :INTEGER;
begin
if (ARequest.PostStream<>NIL) then
if (ARequest.PostStream.SIZE>0) then
BEGIN
AP := TSTRINGLIST.Create;
AP.LoadFromStream( ARequest.PostStream);
S1 := AP.Text;
AP.Free;
CH := Utf8ToAnsi( S1);
FORM1.Memo1.Lines.Add( CH);
//CH接收的中文有乱码
...........

CH := AnsiToUtf8( RD);
AResponse.ResponseNo := 200;
AResponse.ContentType := 'text/html;charset=utf-8';
AResponse.ContentText := CH;
END;
end;


客户端DELPHI7:
FUNCTION IDHTTPSENDCMD2( CONST URL, S :STRING):STRING;
VAR
jsonToSend :TStringStream;
http: TIdcustomHTTP;
begin
jsonToSend := TStringStream.Create( S); //将流位置置为0
jsonToSend.Position := 0;

http := TIdHTTP.Create(nil);
http.HandleRedirects := true;
http.ReadTimeout := 3000;
http.Request.Accept := 'text/html';
http.Request.ContentType := 'text/html;utf-8';

TRY
RESULT := http.Post( URL, jsonToSend);
EXCEPT
RESULT := '';
END;
http.Disconnect;
http.Free;
jsonToSend.Free;
end;

以上代码在接收中文时有乱码,麻烦各位大侠帮看看哪个地方有问题。
...全文
387 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
joman5 2019-04-20
  • 打赏
  • 举报
回复
中文应该需要 urlencode ,服务端接收后再urldecode
doloopcn 2019-04-19
  • 打赏
  • 举报
回复
觉得有点奇怪????
XE7的MEMO本身就支持UTF8
你转换的目的是为了什么????
你把MEMO的编码设置为UTF8测试一下,再搞吧

AP := TSTRINGLIST.Create;
AP.LoadFromStream( ARequest.PostStream);
S1 := AP.Text;
AP.Free;
CH := Utf8ToAnsi( S1);//
FORM1.Memo1.Lines.Add( CH);



引用 楼主 FOREST169 的回复:
服务端DELPHI XE7:
procedure TServerContainer2.DSHTTPService1HTTPTrace(Sender: TObject;
AContext: TDSHTTPContext; ARequest: TDSHTTPRequest;
AResponse: TDSHTTPResponse);
VAR
AP :TSTRINGS;
SIP, SPORT :ANSISTRING;
S1, CH, RD :STRING;
LL :INTEGER;
begin
if (ARequest.PostStream<>NIL) then
if (ARequest.PostStream.SIZE>0) then
BEGIN
AP := TSTRINGLIST.Create;
AP.LoadFromStream( ARequest.PostStream);
S1 := AP.Text;
AP.Free;
CH := Utf8ToAnsi( S1);
FORM1.Memo1.Lines.Add( CH);
//CH接收的中文有乱码
...........

CH := AnsiToUtf8( RD);
AResponse.ResponseNo := 200;
AResponse.ContentType := 'text/html;charset=utf-8';
AResponse.ContentText := CH;
END;
end;


客户端DELPHI7:
FUNCTION IDHTTPSENDCMD2( CONST URL, S :STRING):STRING;
VAR
jsonToSend :TStringStream;
http: TIdcustomHTTP;
begin
jsonToSend := TStringStream.Create( S); //将流位置置为0
jsonToSend.Position := 0;

http := TIdHTTP.Create(nil);
http.HandleRedirects := true;
http.ReadTimeout := 3000;
http.Request.Accept := 'text/html';
http.Request.ContentType := 'text/html;utf-8';

TRY
RESULT := http.Post( URL, jsonToSend);
EXCEPT
RESULT := '';
END;
http.Disconnect;
http.Free;
jsonToSend.Free;
end;

以上代码在接收中文时有乱码,麻烦各位大侠帮看看哪个地方有问题。
BlueStorm 2019-04-19
  • 打赏
  • 举报
回复

procedure TServerContainer2.DSHTTPService1HTTPTrace(Sender: TObject;
  AContext: TDSHTTPContext; ARequest: TDSHTTPRequest;
  AResponse: TDSHTTPResponse);
var
  Buf: TBytes;
  Str: String;
  Stream: TStream
begin
  Stream := ARequest.PostStream;
  if (Stream <> nil) and (Stream.Size > 0) then
  begin
    SetLength(Buf, Stream.Size);
    Stream.Position := 0;
    Stream.Read(Buf, Stream.Size);
    Str := TEncoding.UTF8.GetString(Buf);
    Form1.Memo1.Lines.Add(Str);
    ...........
    AResponse.ResponseNo  := 200;
    AResponse.ContentType := 'text/html;charset=utf-8';
    AResponse.ContentText := Str;
  end;
end;
BlueStorm 2019-04-19
  • 打赏
  • 举报
回复
你说的对,直接用Form1.Memo1.Lines.LoadFromStream(ARequest.PostStream, TEncoding.UTF8))就可以了

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧