xe2 idhttp post乱码的问题

softheaded 2012-09-11 08:47:52
以前用d7、indy9写的程序:用两个函数实现客户端读取服务器端的asp程序生成的xml,一直没什么问题,现在用xe2,用了utf8toansi,utf8decode都会出现乱码,用浏览器却可以正常显示,不知道怎么解决了

function GetdataAsp(AspName:string; StrSql:String):WideString;
begin
try
ConnectUrl := 'Http://'+ConnectIni+AspName;
ReSult := postXml(StrSql,ConnectUrl);
except
on Ex:exception do
begin
ShowMessage(Ex.Message);
assignfile(ErrorF,ExtractFilePath(Application.exename)+'ErrorMessage.txt');
Append(ErrorF);
try
writeln(ErrorF,DateTimeToStr(now)+' | '+Ex.Message);
finally
closefile(ErrorF);
end;
end;
end;
end;

function postXml(const xmlstr, url: WideString): WideString;
var
idHttp:TIdHTTP;
sends:tstrings;
IdEncoderMIME1:TIdEncoderMIME;
vStream: TMemoryStream;
begin
result:='';
try
idHttp:= TIdHTTP.Create(nil);
idHttp.Request.ContentType := 'application/x-www-form-urlencoded';
idhttp.Request.AcceptCharSet := 'UTF-8';
idhttp.Request.AcceptEncoding := 'UTF-8';
idhttp.Request.AcceptLanguage := 'UTF-8';
sends:=tstringlist.Create;
sends.Add('strSQL='+(xmlstr));
result:=idhttp.Post(url,sends);
except
on Ex:exception do
begin
assignfile(ErrorF,ExtractFilePath(Application.exename)+'ErrorMessage.txt');
Append(ErrorF);
try
writeln(ErrorF,DateTimeToStr(now)+' | '+Ex.Message);
finally
closefile(ErrorF);
end;
end;
end;
idHttp.Free;
sends.Free;
...全文
423 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
沉序员 2012-09-16
  • 打赏
  • 举报
回复
是不是真的.我得试下
山东蓝鸟贵薪 2012-09-12
  • 打赏
  • 举报
回复
我来顶顶帖子的
努力学习中
softheaded 2012-09-12
  • 打赏
  • 举报
回复
经过几天的折腾,终于把问题解决了。非常高兴,下面把解决的方法跟大家分享一下:
function GetdataAsp(AspName:string; StrSql:string):string;
var
ErrorF:TEXTFILE;
idHttp:TIdHTTP;
sends:Tstrings;
ResponseStream:TStringStream;
begin
ConnectUrl := 'Http://'+ConnectIni+AspName;
try
// ResponseStream:=TStringstream.Create('',936); //gb2312编码
ResponseStream:=TStringstream.Create('',TEncoding.UTF8);
idHttp:= TIdHTTP.Create(nil);
idhttp.AllowCookies := false;
idhttp.HandleRedirects := false ;
idHttp.Request.ContentType := 'application/x-www-form-urlencoded';
// idHttp.Request.UserAgent:='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)';
// idHTTP.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)';
// idhttp.Request.AcceptCharSet := 'gb2312';
// idhttp.Request.ContentEncoding := 'gb2312';
// idhttp.Request.ContentLanguage := 'gb2312';
// idhttp.Request.AcceptLanguage := 'utf-8';

sends:=tstringlist.Create;
sends.Add('strSQL='+AnsiString(StrSql));
idhttp.Post(ConnectUrl,sends,ResponseStream);
result:=ResponseStream.DataString;
except
on Ex:exception do
begin
assignfile(ErrorF,ExtractFilePath(Application.exename)+'ErrorMessage.txt');
Append(ErrorF);
try
writeln(ErrorF,DateTimeToStr(now)+' | '+Ex.Message);
finally
closefile(ErrorF);
end;
end;
end;
Freeandnil(ResponseStream);
idHttp.Free;
sends.Free;
end;

这是利用服务器端的ASP程序获得和保存数据。不过,在分页的时候又遇到了问题,后来又利用服务器端的webservice的方法把所有的问题都解决了!
softheaded 2012-09-11
  • 打赏
  • 举报
回复
真的解决不了吗?为什么在d7下运行非常正常的程序,在Xe2下却不好用了呢?
softheaded 2012-09-11
  • 打赏
  • 举报
回复
我试过很多种类型,都不正确。现在的英文是正常的,只有中文是乱码,而且我在网上搜了以后尝试用以下的方式

result:=utf8decode(idhttp.Post(url,sends));
result:=utf8toansi(idhttp.Post(url,sends));
服务器端是utf8。都不好用,不知道为什么呢?
JPEXE 2012-09-11
  • 打赏
  • 举报
回复
D2009以前的string是AnsiString,D2009及以后则是UnicodeString,这里注意一下。
softheaded 2012-09-11
  • 打赏
  • 举报
回复
用数据流解决select语句不出现乱码了,但用update的时候又出现了乱码
function postXml(var xmlstr, url: String): String;
var
idHttp:TIdHTTP;
sends:Tstrings;
ErrorF:TEXTFILE;
ResponseStream:TStringStream;
begin
result:='';
try
ResponseStream:=TStringstream.Create('',936);
idHttp:= TIdHTTP.Create(nil);
idhttp.AllowCookies := false;
idhttp.HandleRedirects := false ;
idHttp.Request.ContentType := 'application/x-www-form-urlencoded';
IdHTTP.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)';
sends:=tstringlist.Create;
sends.Add('strSQL='+(xmlstr));
idhttp.Post(url,sends,ResponseStream);
result:=ResponseStream.DataString;
except
end;
Freeandnil(ResponseStream);
idHttp.Free;
sends.Free;
end;

2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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