以前用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;

解决方案 »

  1.   

    D2009以前的string是AnsiString,D2009及以后则是UnicodeString,这里注意一下。
      

  2.   

    我试过很多种类型,都不正确。现在的英文是正常的,只有中文是乱码,而且我在网上搜了以后尝试用以下的方式    result:=utf8decode(idhttp.Post(url,sends));
        result:=utf8toansi(idhttp.Post(url,sends));
    服务器端是utf8。都不好用,不知道为什么呢?
      

  3.   

    用数据流解决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;
      

  4.   

    真的解决不了吗?为什么在d7下运行非常正常的程序,在Xe2下却不好用了呢?
      

  5.   

    经过几天的折腾,终于把问题解决了。非常高兴,下面把解决的方法跟大家分享一下:
    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的方法把所有的问题都解决了!