Delphi 2010中Indy IdHttp Post的问题
我对Indy IdHttp Post方法有问题。 使用Delphi 2007编译的函数CallRpc()工作正常,但使用Delphi 2010编译的相同代码会引发异常 当我将Delphi 2007的IDY TeTHTTP转换为Delphi 2010的IDY TIDHTTP?< /P>时,我必须考虑什么?Delphi 2010中Indy IdHttp Post的问题,delphi,http,unicode,delphi-2010,indy,Delphi,Http,Unicode,Delphi 2010,Indy,我对Indy IdHttp Post方法有问题。 使用Delphi 2007编译的函数CallRpc()工作正常,但使用Delphi 2010编译的相同代码会引发异常 当我将Delphi 2007的IDY TeTHTTP转换为Delphi 2010的IDY TIDHTTP?< /P>时,我必须考虑什么? function CallRpc(const sURL, sXML: string): string; var SendStream : TStream; IdHttp : TI
function CallRpc(const sURL, sXML: string): string;
var
SendStream : TStream;
IdHttp : TIdHttp;
begin
SendStream := TMemoryStream.Create;
IdHttp := TIdHttp.Create(nil);
try
IdHttp.Request.Accept := '*/*';
IdHttp.Request.ContentType := 'text/sXML';
IdHttp.Request.Connection := 'Keep-Alive';
IdHttp.Request.ContentLength := Length(sXML);
StringToStream(sXML, SendStream);
SendStream.Position := 0;
Result := IdHttp.Post(sURL, SendStream);
finally
IdHttp.Free;
SendStream.Free;
end;
end;
增补25.1.2009:
例外情况是:EIdConnClosedGracefully
答复如下:
<?xml version='1.0' encoding='us-ascii'?>
<!DOCTYPE Error [ <!ELEMENT Error (ErrorMessage,DebuggingInfo*)> <!ATTLIST Error Date CDATA #REQUIRED Time CDATA #REQUIRED> <!ELEMENT ErrorMessage (#PCDATA )> <!ELEMENT DebuggingInfo (#PCDATA )> ] >
<Error Date='01/25/2010' Time='08:57:12'>
<ErrorMessage>
XML SERVER ERROR: There was a SYSTEM ERROR error in the Incoming XML Response: $ZE=<UNDEFINED>lexan+196^%eXMLLexAnalyzer
</ErrorMessage>
] >
XML服务器错误:传入的XML响应中存在系统错误:$ZE=UNDEFINEDlexan+196^%eXMLLexAnalyzer
解决方案26.1.2009:
function CallRpc(const sURL, sXML: string): string;
var
SendStream : TStream;
IdHttp : TIdHttp;
sAnsiXML: Ansistring; // <-- new
begin
sAnsiXML := sXML; // <-- new: Implicit string cast
SendStream := TMemoryStream.Create;
IdHttp := TIdHttp.Create(nil);
try
IdHttp.Request.Accept := '*/*';
IdHttp.Request.ContentType := 'text/sXML';
IdHttp.Request.Connection := 'Keep-Alive';
IdHttp.Request.ContentLength := Length(sAnsiXML); // <-- new
SendStream.Write(sAnsiXML[1], Length(sAnsiXML)); // <-- new
SendStream.Position := 0;
Result := IdHttp.Post(sURL, SendStream);
finally
IdHttp.Free;
SendStream.Free;
end;
函数CallRpc(constsurl,sXML:string):string;
变量
SendStream:TStream;
IdHttp:TIdHttp;
sAnsiXML:Ansistring;// 看看做sXML解析是否会有所不同
可能字符串以UTF-16左右的格式传输。内容长度以八位字节为单位,字符串长度以字符为单位。由于Delphi 2009+中的sizeof(Char)=2,这是不匹配的
也许最好将XML转换为UTF8字符串或从UTF8字符串转换为UTF8字符串。某些应用程序不支持USC2 Unicode格式
您应该提供结果流的大小作为ContentLength
更好的方法是:不要提供ContentLength,让Indy为您提供。您的意思是D2007字符串与D2010字符串不同吗?是的。D2009+是unicode,IIRC我也必须复制stringtostream,并使其适应Anistring,D2009+中没有Anistring stringtostream。Karelian,字符串类型的变化是Delphi2010和Delphi2007的主要区别。如果你错过了这一点,你可能会错过很多其他的东西。你最好去看看发行说明。好的。但这个新的unicode字符串对IdHttp Post造成了如此隐藏的影响。我担心还会有什么惊喜。必须用Delphi和Indy发行说明仔细检查我的旧代码的兼容性。我在D2010中发现的另一个惊喜是:RadioGroup的行为异常。似乎我无法在运行时设置它的单选按钮属性。欢迎使用堆栈溢出。请记住包括突出的细节,例如你得到的例外情况,这样读者就不必猜测。当我能够再次使用我的编码计算机并重复错误时,我将包括更多细节。