TIdHTTP to Post XML request and get XML response

11226 views
Skip to first unread message

Rich Rohde

unread,
Nov 30, 2007, 11:35:07 AM11/30/07
to
Objective: Submit to a URL an XML file and get an XML file in response.

Current function:

HTTP:TIdHTTP;

var
XMLRequest, XMLResponse : TStringStream;

HTTP.Post('https://apidemoeu.webex.com/WBXService/XMLService',XMLRequest,XMLResponse);

The response is saying the Document root element is missing.

Any suggestions or ideas?

Regards,
Rich

Eddie Shipman

unread,
Nov 30, 2007, 12:16:52 PM11/30/07
to
Actually, I made slight mistake try this instead. You can see how
things work better. This is tested...

uses ..., MSXML2_TLB, COMObj;

var
oXMLHTTP: IXMLHTTPRequest;
oXMLDoc: IXMLDOMDocument2;
begin
oXMLHTTP := CreateOleObject('MSXML2.ServerXMLHTTP') as
IXMLHTTPRequest;
try
oXMLDoc := CreateOleObject('MSXML2.DOMDocument.3.0') as
IXMLDOMDocument2;
try
oXMLHTTP.open('POST',
'https://apidemoeu.webex.com/WBXService/XMLService', False, '', '');
oXMLHTTP.send(EmptyParam);
finally
oXMLDoc.LoadXML(Trim(oXMLHTTP.ResponseText));
oXMLHTTP := nil;
end;
finally
ShowMessage(oXMLDoc.XML);
oXMLDoc := nil;
end;
end;

Eddie Shipman

unread,
Nov 30, 2007, 12:13:21 PM11/30/07
to
Rich Rohde wrote:

> var
> XMLRequest, XMLResponse : TStringStream;
>
>
>
HTTP.Post('https://apidemoeu.webex.com/WBXService/XMLService',XMLRequest,XMLResponse);

Try this instead:

uses .., MSXML2_TLB, ComObj;


procedure TForm1.GetXML;
var
oXMLHTTP :IXMLHTTPRequest;
begin
oXMLHTTP := CreateOleObject('MSXML2.XMLHTTP') as IXMLHTTPRequest;
try
oXMLHTTP.open('POST',
https://apidemoeu.webex.com/WBXService/XMLService, False, '', '');
oXMLHTTP.send(EmptyParam);
finally
XMLDocument.LoadXML(Trim(oXMLHTTP.ResponseText));
oXMLHTTP := nil;
end;
end;

Rich Rohde

unread,
Nov 30, 2007, 1:50:14 PM11/30/07
to
Eddie,

To clarify, I'm using Delphi 2006. Where can I get MSXML2_TLB?

Rich
"Eddie Shipman" <mr_delphi...@nospamyahoo.com> wrote in message
news:xn0feb6zf...@forums.borland.com...

Rich Rohde

unread,
Nov 30, 2007, 1:55:21 PM11/30/07
to
Additional information:

I'm using NativeXML to create the XMLRequest.

Rich

"Remy Lebeau (TeamB)" <no....@no.spam.com> wrote in message
news:47505a97$1...@newsgroups.borland.com...
>
> "Rich Rohde" <ri...@richware.net> wrote in message
> news:4750...@newsgroups.borland.com...


>
>> The response is saying the Document root element is missing.
>

> Is that what the actual server response data says, or is that what your
> processing of the data says when you try to load it afterwards? What are
> you actually doing with XMLResponse after Post() exits? Did you remember
> to set the Position property back to 0 first?
>
>
> Gambit
>
>

Rich Rohde

unread,
Nov 30, 2007, 1:53:43 PM11/30/07
to
The Actual response is:

<?xml version="1.0" encoding="UTF-8"?>
<serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service"
xmlns:com="http://www.webex.com/schemas/2002/06/common">
<serv:header>
<serv:response>
<serv:result>FAILURE</serv:result>
<serv:reason>Document root element is missing.</serv:reason>
<serv:gsbStatus>PRIMARY</serv:gsbStatus>
<serv:exceptionID>999999</serv:exceptionID>
</serv:response>
</serv:header>
<serv:body/>
</serv:message>

The request is:

<?xml version="1.0" encoding="UTF-8"?>
<serv:message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<header>
<securityContext>
<webExID>**********</webExID>
<password>********</password>
<siteID>********</siteID>
<partnerID>********</partnerID>
</securityContext>
</header>
<body>
<bodyContent
xsi:type="java:com.webex.service.binding.ep.GetAPIVersion"/>
</body>
</serv:message>


"Remy Lebeau (TeamB)" <no....@no.spam.com> wrote in message
news:47505a97$1...@newsgroups.borland.com...
>
> "Rich Rohde" <ri...@richware.net> wrote in message
> news:4750...@newsgroups.borland.com...
>

>> The response is saying the Document root element is missing.
>

Remy Lebeau (TeamB)

unread,
Nov 30, 2007, 1:45:30 PM11/30/07
to

"Rich Rohde" <ri...@richware.net> wrote in message
news:4750...@newsgroups.borland.com...

> The response is saying the Document root element is missing.

Is that what the actual server response data says, or is that what your

Remy Lebeau (TeamB)

unread,
Nov 30, 2007, 3:08:01 PM11/30/07
to

"Rich Rohde" <ri...@richware.net> wrote in message
news:47505ced$1...@newsgroups.borland.com...

> The Actual response is:

There is clearly a root element in that data.

You did not answer my question about how you are using XMLResponse after
Post() exits. So I am guessing that you are passing it as-is to a function
that takes a TStream as input, and did not set the Position property to 0
beforehand. Am I right?


Gambit


Rich Rohde

unread,
Nov 30, 2007, 3:36:48 PM11/30/07
to
> You did not answer my question about how you are using XMLResponse after
> Post() exits. So I am guessing that you are passing it as-is to a
> function that takes a TStream as input, and did not set the Position
> property to 0 beforehand. Am I right?
>

I'm not sure I really understand your question. Before the post the
XMLResponse is created:

XMLResponse := TStringStream.Create('');

After the post, I'm using NativeXML to format the XML to a TString and
showing the information in a TMemo control.

Rich

Remy Lebeau (TeamB)

unread,
Nov 30, 2007, 4:13:14 PM11/30/07
to

"Rich Rohde" <ri...@richware.net> wrote in message
news:47507516$1...@newsgroups.borland.com...

> I'm not sure I really understand your question.

What exactly are you doing with the XMLResponse stream after Post() finishes
its work? Please show that actual code. I don't know how to be any clearer
than that.

> After the post, I'm using NativeXML to format the XML to
> a TString and showing the information in a TMemo control.

But you did not show any of the code you are using to do all of that. You
haven't shown anything you are doing after calling Post(). I have never
looked at NativeXML, so I do not know what kind of input it takes. Does it
accept a TStream object or a String value, for instance? It makes a
difference.


Gambit


Eddie Shipman

unread,
Nov 30, 2007, 4:06:54 PM11/30/07
to
Rich Rohde wrote:

Import the type library.

or possibly:

#import "msxml3.dll"
using namespace MSXML2;

Rich Rohde

unread,
Nov 30, 2007, 5:49:43 PM11/30/07
to
Got it! Just need a little push.

Okay, generated the code as shown and I'm getting nothing with some errors.
Here's the code I used:

procedure TWAMF.GetXML;
var
XMLHTTP : IXMLHTTPRequest;
XMLDoc : IXMLDomDocument2;
const
URL = 'https://apidemoeu.webex.com/WBXService/XMLService';
begin


XMLHTTP := CreateOleObject('MSXML2.ServerXMLHTTP') as IXMLHTTPRequest;
try

XMLDoc := CreateOleObject('MSXML2.DOMDocument.3.0') as
IXMLDOMDocument2;
try

XMLHTTP.open('POST', URL, False, '', '');
XMLHTTP.Op
finally
XMLDoc.LoadXML(Trim(XMLHTTP.ResponseText));
XMLHTTP := nil;
end;
finally
ShowMessage(XMLDoc.xml);
XMLDoc := nil;
end;

end;

Rich Rohde

unread,
Nov 30, 2007, 6:01:31 PM11/30/07
to
Here's the code that usess the TIdHTTP:

function TWAMF.GetXMLstr: boolean;
var
strXML : string;
i : integer;
sl : TStrings;
XMLRqst : string;
XMLResponse : TStringStream;
XMLRequest : TStringStream;

sl := TStringList.Create;
XMLRequest := TStringStream.Create('');
XMLResponse := TStringStream.Create('');

GetVersion(XMLRequest);{ This routine uses NativeXML to create XML
Request }
FormatXML(sl,XMLRequest);
memXML.Lines.Text := sl.Text;
memXML.Lines.Add('<<<<<<<< Response Follows >>>>>>>>>>');
SSLIO.SSLOptions.Method := sslvSSLv3;
HTTP.ReadTimeout := 10000;{ IdTimeoutInfinite; }
HTTP.ConnectTimeout := 10000;
HTTP.IOHandler := SSLIO;
sl.Text := XMLRqst;
HTTP.Request.ContentType := 'text/xml';
HTTP.HTTPOptions := [];
XMLResponse.Position := 0;
XMLRequest.Position := 0;
HTTP.Post(URL,XMLRequest, XMLResponse);
HTTP.Disconnect;
memXML.Lines.Text := memXML.Lines.Text + XMLResponse.DataString;
sl.Free;
XMLRequest.Free;
XMLResponse.Free;
end;

Here's the data in the TMemo field:

<?xml version="1.0" encoding="UTF-8"?>
<serv:message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<header>
<securityContext>

<webExID>***</webExID>
<password>***</password>
<siteID>***</siteID>
<partnerID>***</partnerID>


</securityContext>
</header>
<body>
<bodyContent
xsi:type="java:com.webex.service.binding.ep.GetAPIVersion"/>
</body>
</serv:message>

<<<<<<<< Response follows >>>>>>>>>>


<?xml version="1.0" encoding="UTF-8" ?>
<serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service"
xmlns:com="http://www.webex.com/schemas/2002/06/common">
<serv:header>
<serv:response>
<serv:result>FAILURE</serv:result>
<serv:reason>Document root element is missing.</serv:reason>
<serv:gsbStatus>PRIMARY</serv:gsbStatus>
<serv:exceptionID>999999</serv:exceptionID></serv:response>
</serv:header>
<serv:body>

</serv:body>
</serv:message>

If I comment out the GetVersion function, the following shows in the Memo:

<<<<<<<<<< Response Follows >>>>>>>>>>
<?xml version="1.0" encoding="UTF-8" ?><message
xmlns="http://www.webex.com/schemas/2002/06/service"><header><response><result>SUCCESS</result><reason>:
WebEx XML API V4.0.9, WebEx XML API V4.1.0, WebEx XML API V3.8.2, WebEx
XML API V4.0.1, WebEx XML API V4.0.5, WebEx XML API V4.2.0, WebEx XML API
V4.0.10, WebEx XML API V3.9.1, WebEx XML API
V4.0.7</reason></response></header><body><bodyContent/></body></message>


Remy Lebeau (TeamB)

unread,
Nov 30, 2007, 8:03:36 PM11/30/07
to

"Rich Rohde" <ri...@richware.net> wrote in message
news:47509701$1...@newsgroups.borland.com...

> Here's the code that usess the TIdHTTP:

Since the error is coming from the server's valid response, that means you
are sending invalid XML in your request.

> Here's the data in the TMemo field:

Look very carefully at your request data. When you are using GetVersion(),
the "serv" namespace is not being defined anywhere in the request XML, like
it is in the response XML, so your document element is not valid.


Gambit


Rich Rohde

unread,
Dec 3, 2007, 11:03:36 AM12/3/07
to

> Look very carefully at your request data. When you are using
> GetVersion(),
> the "serv" namespace is not being defined anywhere in the request XML,
> like it is in the response XML, so your document element is not valid.
>
>
> Gambit
>

I would agree with your assessment, however, the following HTML code does
work:

<html><head><title>Test XML API</title>
<script language="javascript">
function submitForm()
{
var url = "http://apidemoeu.webex.com/WBXService/XMLService";
document.form.action = url;
document.form.submit();
}
</script>
</head><body><center><h3>Test XML API</h3>
<form name="form" method="POST" action="">
<b>XML Text:</b><br>
<TEXTAREA NAME="XML" COLS=90 ROWS=30>


<?xml version="1.0" encoding="UTF-8"?>
<serv:message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<header>
<securityContext>

<webExID>**</webExID>
<password>**</password>
<siteID>**</siteID>
<partnerID>**</partnerID>


</securityContext>
</header>
<body>
<bodyContent
xsi:type="java:com.webex.service.binding.ep.GetAPIVersion"/>
</body>
</serv:message>

</TEXTAREA>
<br><br>
<input type="button" value="Test" onclick="submitForm()">
</form></center></body></html>

Regards, Rich

Eddie Shipman

unread,
Dec 3, 2007, 10:25:21 AM12/3/07
to
Rich Rohde wrote:

Indy <i>may</i> be encoding you XML in the request.
Is there a way to check, on your server end, the actual
request text? If it has a bunch of &lt; and &gt; in it,
then that's the problem.

What do the GetVersion and FormatXML function do?
Seems it is going to be easier and more reliable for you to use
XMLHTTPRequest.

Remy Lebeau (TeamB)

unread,
Dec 3, 2007, 4:19:06 PM12/3/07
to

"Rich Rohde" <ri...@richware.net> wrote in message
news:47542990$1...@newsgroups.borland.com...

> I would agree with your assessment, however, the following
> HTML code does work:

That code submits the XML data to the server in a completely different
format than what you are attempting. The equivilent TIdHTTP operation for
that HTML would be to submit a TStringList instead of a TStream, for
example:

var
XMLRequest: TStringList;
XMLResponse : TStringStream;
begin
//...
XMLRequest := TStringList.Create;
try
XMLRequest.Values['XML'] := 'the XML text here';
XMLResponse := TStringStream.Create;
try

HTTP.Post('http://apidemoeu.webex.com/WBXService/XMLService', XMLRequest,
XMLResponse);
// use XMLResponse as needed...
finally
XMLResponse.Free;
end;
finally
XMLRequest.Free;
end;
//...
end;


Gambit


Remy Lebeau (TeamB)

unread,
Dec 3, 2007, 4:21:42 PM12/3/07
to

"Eddie Shipman" <mr_delphi...@nospamyahoo.com> wrote in message
news:xn0fei4vf...@forums.borland.com...

> Indy <i>may</i> be encoding you XML in the request.

When sending a TStream, the request data is not encoded at all. Encoding
only takes place when sending a TStrings instead.


Gambit


Remy Lebeau (TeamB)

unread,
Dec 3, 2007, 4:22:17 PM12/3/07
to

"Rich Rohde" <ri...@richware.net> wrote in message
news:47505d4f$1...@newsgroups.borland.com...

> I'm using NativeXML to create the XMLRequest.

We already knew that, but you never showed HOW you are using it to create
the request XML. It doesn't matter at this point, though.


Gambit


Rich Rohde

unread,
Dec 4, 2007, 11:20:07 AM12/4/07
to
I've found the problem.

[Note: using NativeXML]

Before saving the XML I had set ExternalEncoding the property to use UTF-8
causing some un-needed charcters in the TStream. Removed the
ExternalEncoding property and used EncodingString property (EncodingString
:= 'UTF-8') and problem resolved.

I knew this had to be simple, but was persuing the wrong direction.

Thanks for everyone's help. The comment about encoding is what started me
moving in the right direction.

R

Reply all
Reply to author
Forward
0 new messages