 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Atle Smelvær Guest
|
Posted: Wed Sep 21, 2005 12:58 pm Post subject: IdHTTP Post with values containing " |
|
|
I can't get IdHTTP.Post to work when using values containing ". Look at this
code:
const
XMLMessage = '<?xml version=''1.0'' encoding=''ISO-8859-1''?>'+
'<messages from="PROVIDER" time="20050916 104800">'+
'<message provider="1" price_category="CPA000">'+
'<billing number="%s"/>'+
'<recipient number="%0:s"/>'+
'<text>%s</text>'+
'<validityperiod days="0" hours="0" minutes="5"/>'+
'</message>'+
'</messages>';
var
str: string;
List: TStringList;
ResponseStream: TStringStream;
begin
List := TStringList.Create;
ResponseStream := TStringStream.Create('');
try
str := Format(XMLMessage,[Edit1.Text,Memo1.Lines.Text]);
IdHTTP1.Post('http://someplace.com/servlet/Send',List,ResponseStream);
Memo2.Lines.Text := ResponseStream.DataString;
finally
List.Free;
ResponseStream.Free;
end;
end;
Problem is that when Post is done, the it treats the value as several
values, putting & inside and corrupting the XML. Is there a way to fix this?
Problem is, if I change " to %22 it will be posted as %22 and I need it to
be converted back, or have another solution that handles " properly.
-Atle
|
|
| Back to top |
|
 |
Atle Smelvær Guest
|
Posted: Wed Sep 21, 2005 1:52 pm Post subject: Re: IdHTTP Post with values containing " |
|
|
Deleted to mutch of the source there, replace this:
str := Format(XMLMessage,[Edit1.Text,Memo1.Lines.Text]);
with this:
List.Values['messages'] := Format(XMLMessage,[Edit1.Text,Memo1.Lines.Text]);
-Atle
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Wed Sep 21, 2005 5:36 pm Post subject: Re: IdHTTP Post with values containing " |
|
|
"Atle Smelvær" <atle (AT) datagrafikk (DOT) no> wrote
| Quote: | I can't get IdHTTP.Post to work when using values containing ".
|
That is because you are not posting the data correctly in the first place.
| Quote: | XMLMessage = '<?xml version=''1.0'' encoding=''ISO-8859-1''?>'+
snip |
You cannot use Post(TStrings) to post XML data. You need to use
Post(TStream) instead. You also need to set the ContentType of the Request
(if you are not already). Try this code instead:
var
PostStream: TStringStream;
begin
PostStream :=
TStringStream.Create(Format(XMLMessage,[Edit1.Text,Memo1.Lines.Text]));
try
IdHTTP1.Request.ContentType := 'text/xml';
Memo2.Lines.Text :=
IdHTTP1.Post('http://someplace.com/servlet/Send', PostStream);
finally
PostStream.Free;
end;
end;
Also, don't forget to validate that your TEdit and TMemo content is
XML-friendly as well.
| Quote: | Problem is that when Post is done, the it treats the value as
several values, putting & inside and corrupting the XML.
|
As it should be, because that is what Post(TStrings) is designed to do. It
is not meant to send XML data in the first place. It is designed to send
url-encoded form submission data instead.
Gambit
|
|
| Back to top |
|
 |
Atle Smelvær Guest
|
Posted: Thu Sep 22, 2005 6:52 am Post subject: Re: IdHTTP Post with values containing " |
|
|
I posted it with a stream, but had to change the XMLMessage to contain
"messages=" first. Good enough for the first part, but then there is some
converting that needs to be done. Is there a good URLEncode function inside
Indy9. The TIdUri.PathEncode does not convert properly the way I want it. ?
is not converted, and spaces are converted to %20 instead of +.
-Atle
|
|
| Back to top |
|
 |
Atle Smelvær Guest
|
Posted: Fri Sep 23, 2005 12:42 pm Post subject: Re: IdHTTP Post with values containing " |
|
|
Just in case someone wondered. Did not find any good function inside Indy,
so I used a little snippet from the internet:
function URLEncode(const S: string; const InQueryString: Boolean): string;
var
i: integer;
begin
Result := '';
for i := 1 to length(s) do
begin
case s[i] of
'A'..'Z',
'a'..'z',
'0'..'9',
'-', '_', '.': result := result + s[i];
' ': if InQueryString then
result := result + '+'
else
result := result + '%20';
else
result := result + '%' + SysUtils.IntToHex(Ord(s[i]), 2);
end;
end;
end;
And ofcourse, setting
TIdHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
is very important... And used MSXML to create the XML data instead, don't
matter that mutch where I will use this. Other than that, it works like a
charm.
-Atle
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|