I am facing an error "HTTP/1.1 400" after XML parsing error with message "Cannot find the declaration of element 'XML_Element'". The posts I found in the web about the subject didn't help so any comment about my below code will be really appreciated.
File .cpp
void __fastcall TForm2::Button1Click(TObject *Sender)
{Button1->Cursor = crHourGlass;
int IX0BBClick = 0;
_di_IXMLDocument XMLDocument1 = interface_cast<Xmlintf::IXMLDocument> (new TXMLDocument(NULL));
_di_IXMLDocument XMLDocument2 = interface_cast<Xmlintf::IXMLDocument> (new TXMLDocument(NULL));
UnicodeString US0Reponse, US0URl = "http://Marin-ml.ubiMarin-senegal.com";
try {//- Document XML #1 = Demande|Request
XMLDocument1->Active = true;
XMLDocument1->Version = "1.0";
XMLDocument1->Encoding = "UTF-8";
XMLDocument1->Options = XMLDocument1->Options <<doNodeAutoIndent >>doNamespaceDecl >>doAutoPrefix;
//- DMC_WRAPPER
_di_IXMLNode XN0Wrapper = XMLDocument1->CreateNode("DMC_WRAPPER");
XMLDocument1->SetDocumentElement(XN0Wrapper);
XMLDocument1->DocumentElement->DeclareNamespace("", "urn:x-srp:fr.srp.protocole:enveloppe-v30");
XMLDocument1->DocumentElement->DeclareNamespace("xsi", "https://www.w3.org/2001/XMLSchema-instance");
//- BODY under DMC_WRAPPER
_di_IXMLNode XN0WBody = XMLDocument1->DocumentElement->AddChild("BODY", "urn:x-srp:fr.srp.protocole:enveloppe-v30");
//- SHIP_MESSAGE
_di_IXMLNode XN0WBMShip = XN0WBody->AddChild("SHIP_MESSAGE", "urn:x-srp:fr.srp.protocole:message-v30");
if(XN0WBMShip)
{
//\ SHIP_MESSAGE
//\ BODY under DMC_WRAPPER
//\ DMC_WRAPPER
//- Document XML #1 = Demande|Request
TIdHTTP *IdHTTP1 = new TIdHTTP(this);
TIdSSLIOHandlerSocketOpenSSL *LHandler = new TIdSSLIOHandlerSocketOpenSSL(this);
IdHTTP1->IOHandler = LHandler;
TIdHashMessageDigest5 *MD5Digest = new TIdHashMessageDigest5;
UnicodeString UX0Content_MarinaML = XMLDocument1->XML->Text + "MySecretData";
UX0Content_MarinaML = MD5Digest->HashStringAsHex(UX0Content_MarinaML);
//- Initialisation HTTP
IdHTTP1->Request->CustomHeaders->AddValue("Content-MarinML", UX0Content_MarinML);
IdHTTP1->Request->CustomHeaders->AddValue("Version-MarinML", "3.0.0.0");
IdHTTP1->Request->CustomHeaders->AddValue("Origin", "http://myurl.com");
IdHTTP1->Request->UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100102 Firefox/12.0";
IdHTTP1->Request->Accept = "*/*";
IdHTTP1->Request->AcceptLanguage = "en-US,en;q=0.8";
IdHTTP1->Request->ContentType = "text/xml; charset=\"UTF-8\"";
//\ Initialisation HTTP
XMLDocument1->SaveToFile(Form1->UX0CPDOrthassis + "\\MEDMAR00.xml");
TStringStream *SS0REBody = new TStringStream(XMLDocument1->XML->Text);
TMemoryStream *MS0Response = new TMemoryStream();
IdLogStream1 = new TIdLogStream(NULL); // Débogage | Debugging
IdLogStream1->ReceiveStream = new TStringStream(); // Débogage | Debugging
IdLogStream1->FreeStreams = true; // Débogage | Debugging
IdLogStream1->Active = true; // Débogage | Debugging
IdLogStream1->ReplaceCRLF = false; // Débogage | Debugging
IdHTTP1->Intercept = IdLogStream1; // Débogage | Debugging
RichEdit1->Clear(); // Débogage | Debugging
//\ Exécution requête HTTP
IdHTTP1->Post(US0URl, SS0REBody, MS0Response);
//- Exécution requête HTTP
//- Document XML #2 = Réponse|Response
XMLDocument2->LoadFromStream(MS0Response);
//\ Document XML #2 = Réponse|Response
RichEdit1->Clear();
RichEdit1->Lines->Add(XMLDocument1->XML->Text);
RichEdit1->Lines->Add("//-------------------");
RichEdit2->Clear();
RichEdit2->Lines->Add(XMLDocument2->XML->Text);
RichEdit2->Lines->Add("//-------------------");
}
else
{XMLDocument1->XML->Text = "XN0WBMShip not assigned!";
}
XMLDocument2->SaveToFile(Form1->UX0CPDOrthassis + "\\MERMAR00.xml");
IX0BBClick = 1;
}
catch(EIdHTTPProtocolException &EX0Vl){IdLogStream1->ReceiveStream->Seek(0, soFromBeginning);
RichEdit1->Lines->LoadFromStream(IdLogStream1->ReceiveStream);
Form1->AX0LMes = RichEdit1->Lines->Text;
Form1->IKAf0ORTErr();
}
catch(ERemotableException &EX0Vl){Form1->AX0LMes = EX0Vl.Message; Form1->IKAf0ORTErr();}
catch(EDatabaseError &EX0Vl) {Form1->AX0LMes = EX0Vl.Message; Form1->IKAf0ORTErr();}
catch(Exception &EX0Vl) {Form1->AX0LMes = EX0Vl.Message; Form1->IKAf0ORTErr();}
catch(...) {Form1->AX0LMes = "Exception non gérée."; Form1->IKAf0ORTErr();}
Button1->Cursor = crDefault;
}
Sent message : MEDMAR00.xml
<?xml version="1.0" encoding="UTF-8"?>
<DMC_WRAPPER xmlns="urn:x-srp:fr.srp.protocole:enveloppe-v30" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance">
<BODY>
<SHIP_MESSAGE xmlns="urn:x-srp:fr.srp.protocole:message-v30"/>
</BODY>
</DMC_WRAPPER>
Error log : IdLogStream1->ReceiveStream
<!doctype html><html lang="en"><head><title>HTTP Status 400 – Bad
Request</title><style type="text/css">body {font-
family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-
color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-
size:14px;} p {font-size:12px;} a {color:black;} .line
{height:1px;background-color:#525D76;border:none;}</style></head>
<body><h1>HTTP Status 400 – Bad Request</h1><hr class="line" />
<p><b>Type</b> Status Report</p><p><b>Message</b> Erreur :
[DMCRequest] Erreur de parsing XML pour la requête DMC : Error on
line 2: cvc-elt.1.a: Cannot find the declaration of element &#
39;DMC_WRAPPER'.
Message MarinaML = [<?xml version="1.0"?>
<DMC_WRAPPER xmlns="urn:x-srp:fr.srp.protocole:enveloppe-
v30" xmlns:xsi="https://www.w3.org/2001&#
47;XMLSchema-instance">
<BODY>
<SHIP_MESSAGE xmlns="urn:x-
srp:fr.srp.protocole:message-v30"/>
</BODY>
</DMC_WRAPPER>
]</p><p><b>Description</b> The server cannot or will not process the
request due to something that is perceived to be a client error (e.g.,
malformed request syntax, invalid request message framing, or deceptive
request routing).</p><hr class="line" /><h3>Apache Tomcat/9.0.56
</h3></body></html>
<!--
2024/02/13 - 10:33:28, TEMarinaml/1219001 : HTTP/1.1 400
Server: nginx/1.7.6
Date: Tue, 13 Feb 2024 10:33:24 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 1330
Connection: keep-alive
Content-Language: en
-->