Board index » delphi » TIdHTTP to Post XML request and get XML response
Rich Rohde
![]() Delphi Developer |
Rich Rohde
![]() Delphi Developer |
TIdHTTP to Post XML request and get XML response2007-12-01 12:35:07 AM delphi220 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
![]() Delphi Developer |
2007-12-01 01:13:21 AM
Re:TIdHTTP to Post XML request and get XML response
Rich Rohde writes:
Quotevar 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; |
Eddie Shipman
![]() Delphi Developer |
2007-12-01 01:16:52 AM
Re:TIdHTTP to Post XML request and get XML response
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; |
Remy Lebeau (TeamB)
![]() Delphi Developer |
2007-12-01 02:45:30 AM
Re:TIdHTTP to Post XML request and get XML response
"Rich Rohde" <XXXX@XXXXX.COM>writes
QuoteThe response is saying the Document root element is missing. you actually doing with XMLResponse after Post() exits? Did you remember to set the Position property back to 0 first? Gambit |
Rich Rohde
![]() Delphi Developer |
2007-12-01 02:50:14 AM
Re:TIdHTTP to Post XML request and get XML response
Eddie,
To clarify, I am using Delphi 2006. Where can I get MSXML2_TLB? Rich "Eddie Shipman" <XXXX@XXXXX.COM>writes QuoteActually, I made slight mistake try this instead. You can see how |
Rich Rohde
![]() Delphi Developer |
2007-12-01 02:53:43 AM
Re:TIdHTTP to Post XML request and get XML response
The Actual response is:
<?xml version="1.0" encoding="UTF-8"?> <serv:message xmlns:serv="www.webex.com/schemas/2002/06/service" xmlns:com="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="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)" <XXXX@XXXXX.COM>writes Quote
|
Rich Rohde
![]() Delphi Developer |
2007-12-01 02:55:21 AM
Re:TIdHTTP to Post XML request and get XML response
Additional information:
I'm using NativeXML to create the XMLRequest. Rich "Remy Lebeau (TeamB)" <XXXX@XXXXX.COM>writes Quote
|
Remy Lebeau (TeamB)
![]() Delphi Developer |
2007-12-01 04:08:01 AM
Re:TIdHTTP to Post XML request and get XML response
"Rich Rohde" <XXXX@XXXXX.COM>writes
QuoteThe Actual response is: 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
![]() Delphi Developer |
2007-12-01 04:36:48 AM
Re:TIdHTTP to Post XML request and get XML responseQuoteYou did not answer my question about how you are using XMLResponse after XMLResponse := TStringStream.Create(''); After the post, I am using NativeXML to format the XML to a TString and showing the information in a TMemo control. Rich |
Eddie Shipman
![]() Delphi Developer |
2007-12-01 05:06:54 AM
Re:TIdHTTP to Post XML request and get XML response
Rich Rohde writes:
QuoteEddie, #import "msxml3.dll" using namespace MSXML2; |
Remy Lebeau (TeamB)
![]() Delphi Developer |
2007-12-01 05:13:14 AM
Re:TIdHTTP to Post XML request and get XML response
"Rich Rohde" <XXXX@XXXXX.COM>writes
QuoteI'm not sure I really understand your question. than that. QuoteAfter the post, I am using NativeXML to format the XML to 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 |
Rich Rohde
![]() Delphi Developer |
2007-12-01 06:49:43 AM
Re:TIdHTTP to Post XML request and get XML response
Got it! Just need a little push.
Okay, generated the code as shown and I am 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
![]() Delphi Developer |
2007-12-01 07:01:31 AM
Re:TIdHTTP to Post XML request and get XML response
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; const URL = 'https://apidemoeu.webex.com/WBXService/XMLService'; begin 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="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="www.webex.com/schemas/2002/06/service" xmlns:com="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="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)
![]() Delphi Developer |
2007-12-01 09:03:36 AM
Re:TIdHTTP to Post XML request and get XML response
"Rich Rohde" <XXXX@XXXXX.COM>writes
QuoteHere's the code that usess the TIdHTTP: QuoteHere's the data in the TMemo field: it is in the response XML, so your document element is not valid. Gambit |
Eddie Shipman
![]() Delphi Developer |
2007-12-03 11:25:21 PM
Re:TIdHTTP to Post XML request and get XML response
Rich Rohde writes:
QuoteHere's the code that usess the TIdHTTP: request text? If it has a bunch of < and>in it, then that is 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. |