IdHttp Post 方法 Delphi 2010
和我之前的其他人一样,我在 Delphi 2010 中使用 IdHttp(Indy 10.5.5) 组件时遇到了麻烦。该代码在 Delphi 7 中工作正常:
var
XMLString : AnsiString;
lService : AnsiString;
ResponseStream: TMemoryStream;
InputStringList : TStringList;
begin
ResponseStream := TMemoryStream.Create;
InputStringList := TStringList.Create;
XMLString :='<?xml version="1.0" encoding="ISO-8859-1"?> '+
'<!DOCTYPE pnet_imessage_send PUBLIC "-//PeopleNet//pnet_imessage_send" "http://open.peoplenetonline.com/dtd/pnet_imessage_send.dtd"> '+
'<pnet_imessage_send> '+
' <cid>username</cid> '+
' <pw>password</pw> '+
' <vehicle_number>tr01</vehicle_number> '+
' <deliver>now</deliver> '+
' <action> '+
' <action_type>reply_with_freeform</action_type> '+
' <urgent_reply>yes</urgent_reply> '+
' </action> '+
' <freeform_message>Test Message Version 2</freeform_message> '+
'</pnet_imessage_send> ';
lService := 'imessage_send';
InputStringList.Values['service'] := lService;
InputStringList.Values['xml'] := XMLString;
try
IdHttp1.Request.Accept := '*/*';
IdHttp1.Request.ContentType := 'text/XML';
IdHTTP1.Post('http://open.peoplenetonline.com/scripts/open.dll', InputStringList, ResponseStream);
...
finally
ResponseStream.Free;
InputStringList.Free;
end;
到目前为止,此代码与 D7 代码之间的唯一区别是我更改了String类型改为AnsiString,并添加了HTTP Request属性。
我从服务器返回的响应是“XML 无法解析”。 Line:1 Position: 19' 处预计有空格,我假设 XML 在该过程中的某个地方出现了乱码,但我不知道哪里出错了。
有什么想法吗?
Like others before me, I'm having troubles using the IdHttp(Indy 10.5.5) component in Delphi 2010. The code works fine in Delphi 7:
var
XMLString : AnsiString;
lService : AnsiString;
ResponseStream: TMemoryStream;
InputStringList : TStringList;
begin
ResponseStream := TMemoryStream.Create;
InputStringList := TStringList.Create;
XMLString :='<?xml version="1.0" encoding="ISO-8859-1"?> '+
'<!DOCTYPE pnet_imessage_send PUBLIC "-//PeopleNet//pnet_imessage_send" "http://open.peoplenetonline.com/dtd/pnet_imessage_send.dtd"> '+
'<pnet_imessage_send> '+
' <cid>username</cid> '+
' <pw>password</pw> '+
' <vehicle_number>tr01</vehicle_number> '+
' <deliver>now</deliver> '+
' <action> '+
' <action_type>reply_with_freeform</action_type> '+
' <urgent_reply>yes</urgent_reply> '+
' </action> '+
' <freeform_message>Test Message Version 2</freeform_message> '+
'</pnet_imessage_send> ';
lService := 'imessage_send';
InputStringList.Values['service'] := lService;
InputStringList.Values['xml'] := XMLString;
try
IdHttp1.Request.Accept := '*/*';
IdHttp1.Request.ContentType := 'text/XML';
IdHTTP1.Post('http://open.peoplenetonline.com/scripts/open.dll', InputStringList, ResponseStream);
...
finally
ResponseStream.Free;
InputStringList.Free;
end;
The only differences so far between this and the D7 code is that I've changed the String types to AnsiString, and added the HTTP Request properties.
The response I get back from the server is 'XML failed to parse. Whitespace expected at Line:1 Position: 19', I'm assuming the XML got garbled up somewhere in the process, but I can't figure our where I'm going wrong.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,Post() 的 TStrings 版本根据“application/x-www-form-urlencoded”内容类型对输入数据进行编码,但您将 ContentType 设置为“text/xml”,即使您实际上并非如此自行发布原始 XML 数据。如果您没有在 D7 代码中设置 ContentType,则 TIdHTTP 会为您将 ContentType 设置为“application/x-www-form-urlencoded”。您需要在 D2010 代码中镜像相同的行为,方法是自己设置相同的 ContentType 值,或者再次删除分配,以便 TIdHTTP 可以再次为您执行此操作。
The TStrings version of Post() encodes the input data according to the 'application/x-www-form-urlencoded' content type by default, but you are setting the ContentType to 'text/xml' instead, even though you are not actually posting raw XML data by itself. If you were not setting the ContentType in your D7 code, then TIdHTTP was setting the ContentType to 'application/x-www-form-urlencoded' for you. You need to miror that same behavior in your D2010 code, either by setting the same ContentType value yourself, or by removing the assignment again so TIdHTTP can do it for you again.