🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.web
Article #552

Re: sending xml using TIdHTTP

#552
From: Remy Lebeau (TeamB)
Date: Sun, 19 Mar 2006 00:09
82 lines
3643 bytes
"Hamid" <hrz_mir@Yahoo.com> wrote in message
news:441c41ec$1@newsgroups.borland.com...

> When I use the Post method (Regardless which overloaded method
> I'm using) it passes the xml content to the server successfully.

That is not what you said earlier.

> But the problem arises when I try to parse the received message with a xml
parser.

You did not say anything about that earlier.

> The content contains some special characters. For example the first line :
> <?xml version="1.0" encoding="utf-8"?>. Now I do not know if I use an
> Encoding / Decoding before and after passing the content to the server?

You have to encode the XML before sending it to the server, and decode it
after receiving it.  TIdHTTP does not encode/decode it for you.

> If yes, which encoding method I should use?

The XML prolog states the encoding of the XML content - in this case, the
content is encoded with UTF-8 (which is the default encoding for XML
anyway).

> The URLEncode method encodes only the texts that have the special form
> of a URL, not a content.

You should not be using URLEncode() for XML at all, and you should not be
using any Post() method that calls ULREncode() internally (the ones that
take TStrings as an input parameter).  Pass your input XML as a TStream, and
then receive the output XML as a TStream.  That way, your input data, and
the output data, are not touched by TIdHTTP at all, meaning that they are
transmitted as-is.  For example:

    TMemoryStream *pInputXML = new TMemoryStream;
    try
    {
        // load pInputXML with your raw UTF-8 encoded XML as needed....

        TMemoryStream *pOutputXML = new TMemoryStream;
        try
        {
            IdHTTP1->Request->ContentType = "text/xml";
            IdHTTP->Post("http://yoursite.com", pInputXML, pOutputXML);

            // use the raw UTF-8 encoded XML from pOutputXML as needed...
        }
        __finally
        {
            delete pOutputXML;
        }
    }
    __finally
    {
        delete pInputXML;
    }


Alternatively, since UTF-8 can fit into an AnsiString, you can do the
following instead:

    TStringStream *pInputXML = new TStringStream("");
    try
    {
        // load pInputXML with your raw UTF-8 encoded XML as needed....

        IdHTTP1->Request->ContentType = "text/xml";
        AnsiString sOutputXML = IdHTTP->Post("http://yoursite.com",
pInputXML);

        // use the raw UTF-8 encoded XML from sOutputXML as needed...
    }
    __finally
    {
        delete pInputXML;
    }


Gambit
Technical Details
Message-ID: <441ca28d@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <441519b5$1@newsgroups.borland.com> <44152ae8$1@newsgroups.borland.com> <4415306d$1@newsgroups.borland.com> <4415e49d$1@newsgroups.borland.com> <4417bf88$1@newsgroups.borland.com> <441856a5$3@newsgroups.borland.com> <441c41ec$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: feeec,8fda7960f12a26ec
X-Google-Attributes: gidfeeec,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.web
References: <441519b5$1@newsgroups.borland.com> <44152ae8$1@newsgroups.borland.com> <4415306d$1@newsgroups.borland.com> <4415e49d$1@newsgroups.borland.com> <4417bf88$1@newsgroups.borland.com> <441856a5$3@newsgroups.borland.com> <441c41ec$1@newsgroups.borland.com>
Subject: Re: sending xml using TIdHTTP
Date: Sat, 18 Mar 2006 16:09:57 -0800
Lines: 84
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <441ca28d@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1142727309 68.69.17.41 (18 Mar 2006 16:15:09 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail