IdHttp发布方法Delphi 2010 [英] IdHttp Post Method Delphi 2010

查看:73
本文介绍了IdHttp发布方法Delphi 2010的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和我之前的其他人一样,我在Delphi 2010中使用IdHttp(Indy 10.5.5)组件时遇到了麻烦。代码在Delphi 7中正常工作:

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;

到目前为止,此代码与D7代码之间的唯一区别是我将String类型更改为AnsiString,并添加了HTTP Request属性。

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.

我从服务器返回的响应是XML解析失败。 Whitespace预期在Line:1 Position:19'处出现,我假设XML在处理过程中出现了乱码,但我无法弄清楚哪里出了问题。

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.

有什么想法吗?

推荐答案

TStrings版本的Post()根据'application / x-www-form-urlencoded'对输入数据进行编码内容类型默认情况下,但是您实际上将ContentType设置为 text / xml,即使您实际上并未单独发布原始XML数据。如果您未在D7代码中设置ContentType,则TIdHTTP会为您将ContentType设置为 application / x-www-form-urlencoded。您需要通过自己设置相同的ContentType值或再次删除分配,在D2010代码中镜像相同的行为,以便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.

这篇关于IdHttp发布方法Delphi 2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆