IdHTTP.Post with multi parameters

IdHTTP.Post with multi parameters

Post by Paolo Carr » Thu, 02 Oct 2003 18:46:54


I must format post informations in TIdHTTP.post method passing 5 parameters
(4 string and one is the text of an XML file).
I make it in this way:

var
Data: TStringList;
PostRes: TStringStream;
Address: String;
XMLFile: TXMLDocument;
begin
XMLFile.LoadFromFile(PathAndNameFile);
PosRes := TStringStream.Create('');
Data := TStringList.Create;

IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';

Data.Add('user=' + UserString +
'&password=' + PasswordString +
'&requestXML=' + XMLFile.XML.Text +
'¶m4=S' +
'&Operation=' + CodeOperation + '&');
Address := http://URLAddress/service;
try
IdHTTP1.Post(Address, Data, PostRes);
except
end;
end;

It works fine, but it doesn't if in the XML file there is the "&" character.
Does someone knows how to avoid this problem and post the parameters also if
they contain the "&" character?
Thanks for your help.
Frank
 
 
 

IdHTTP.Post with multi parameters

Post by Team » Fri, 03 Oct 2003 02:27:23


That is not correct. You need to use a separate call to Add() for each
value you want to send, ie:

Data.Add('user=' + UserString);
Data.Add('password=' + PasswordString);
Data.Add('requestXML=' + XMLFile.XML.Text);
Data.Add('param4=S');
Data.Add('Operation=' + CodeOperation);

Alternatively, have a look at the TIdMultiPartDataStream class instead. It
is better suited for posting data via HTTP.

character.

'&' is not valid for XML. It needs to be converted to '&' instead, as
per the XML standard. I'm surprised TXMLDocument let that slip by - it
shouldn't have. However, as a side note, I see that you are not actually
instantiating the TXMLDocument instance at all. Have a look at the
LoadXMLDocument() function instead of using TXMLDocument::LoadFromFile()
directly.


Gambit

 
 
 

IdHTTP.Post with multi parameters

Post by Paolo Carr » Fri, 03 Oct 2003 16:14:12

Thanks for the answer.




I tried in this mode, but the server application reply saying that there are
not all the parameters.

It

I think I don't have TIdMultiPartDataStream class: I'm working with Delphi 6
and Indy 8.0.25. Is there another solution?


I know that. My TXMLDocument is correct. The character '&' is however
present because if you replace it with '&' the character '&' is still
present.


My code was extract from my application and I deleted many rows to leave
only those are interesting for my problem: this is the reason you don't see
the TXMLDocument instance; in my application it there is.

Paolo
 
 
 

IdHTTP.Post with multi parameters

Post by Team » Fri, 03 Oct 2003 16:35:39


Yes - upgrade. That version of Indy is very outdated. The latest version
of Indy is v9.0.14. You can get it from http://www.yqcomputer.com/


Then you have a problem. Because the only other legal ways to represent the
"&" character is via either "&" or "&" instead of "&", but as
you can see, they all include "&", so no matter what the XML is, you're
going to break your HTTP posting, unless you encode the XML data using
something like Base64, but then the server needs to be able to decode the
data back to its original format after receiving it. Since using TStrings
is not going to work for the kind of data you are trying to post, again I
strongly suggest that you upgrade so that you can make use of the
TIdMultiPartDataStream class, which does not suffer from this problem, I
don't think.


Gambit
 
 
 

IdHTTP.Post with multi parameters

Post by Paolo Carr » Sat, 04 Oct 2003 17:06:05

Thanks for the new answer.
I upraded Indy components to version 9.0.14.
I modified my code into:

var
Data: TIdMultiPartFormDataStream;
PostRes: TStringStream;
Address: String;
begin
PosRes := TStringStream.Create('');
Data := TIdMultiPartFormDataStream.Create
IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
Data.AddFormField('user', UserString);
Data.AddFormField('password', PasswordString);
Data.AddFile('requestXML', PathAndNameFile, "text/xml");
Data.AddFormField('param4', 'S');
Data.AddFormField('Operation', CodeOperation);
Address := http://URLAddress/service;
try
IdHTTP1.Post(Address, Data, PostRes);
except
end;
// ...
end;

But the server application respond saying that it doesn't receive all of
parameters (I'm trying to contact server administrator to know if the
problem is in server application).
Is my code correct?
Thanks for your patience.
Paolo


"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM > ha scritto nel messaggio



the
 
 
 

IdHTTP.Post with multi parameters

Post by Paolo Carr » Thu, 09 Oct 2003 16:29:18


"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM > ha scritto nel messaggio

the

Only for information: I resolved the problem encoding the XML string with
the function HTTPEncode (HTTPApp.pas).
Paolo