EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Send SOAPMessage through SOAPClient with utf-8

Also by EldoS: CallbackDisk
Create virtual disks backed by memory or custom location, expose disk images as disks and more.
#37556
Posted: 08/25/2016 07:05:00
by Marek Dostal (Premium support level)
Joined: 08/11/2016
Posts: 5

I tried to find answer to my problem, but didn't find it. I need to send SOAPMessage through SOAPClient (TElXMLSOAPClient). I used an example project from XMLBlackBox/SOAPClient. It works, but the message, which is sent has always encoding utf-16 at the beginning of xml: <?xml version="1.0" encoding="UTF-16"?>, but I need there "UTF-8": <?xml version="1.0" encoding="UTF-8"?>. How can I get it? I tried set SOAPClient.Encoding:='utf-8', but it has no effect.
#37557
Posted: 08/25/2016 08:07:11
by Eugene Mayevski (EldoS Corp.)

Thank you for contacting us.

Could you please show a code snippet of how you use this component, so that we could better understand, what's going on and what you mean by sending UTF-16.

On a side note it would help a lot if you used CODE button located above the text entry box (alternatively you can write [ CODE ] and [ /CODE ] tags by hand) to mark the beginning and the end of the code blocks in your messages. This would enable syntax highlighting and line numbering on the code and make it easier for analysis.


Sincerely yours
Eugene Mayevski
#37559
Posted: 08/25/2016 08:19:16
by Marek Dostal (Premium support level)
Joined: 08/11/2016
Posts: 5

Thank You for answer! Here is source code (derived from sample):

Code
procedure TfrmMain.Button1Click(Sender: TObject);
var hpXML: TXMLDocument;
    hpXMLDocument: TElXMLDOMDocument;
    hpStream: TMemoryStream;
begin
  btnExploreRequestXML.Enabled := false;
  btnExploreResponseXML.Enabled := false;

  try
    FSOAPClient.SOAPPrefix := 'soap';
    FSOAPClient.SOAPVersion := SOAP_v1_2;

    //load xml from file to TXMLDocument:
    hpXML:=TXMLDocument.Create(nil);
    hpXml.LoadFromFile('c:\temp\test.xml');

    //load xml document from TXMLDocument to TElXMLDOMDocument:
    hpXMLDocument:=TElXMLDOMDocument.Create;
    hpStream:=TMemoryStream.Create;
    hpXML.Active:=true;
    hpXML.SaveToStream(hpStream);
    hpStream.Position:=0;
    hpXMLDocument.LoadFromStream(hpStream);

    //load message from TElXMLDOMDocument to FSOAPClient:
    FSOAPClient.Encoding:='UTF-8';
    FSOAPClient.LoadMessage(hpXMLDocument);
    FSOAPClient.HTTPClient := HTTPSClient;
    FSOAPClient.URL := 'https://pg.eet.cz:443/eet/services/EETServiceSOAP/v2';
    FSOAPClient.SendMessage;

    btnExploreRequestXML.Enabled := true;
    btnExploreResponseXML.Enabled := (FSOAPClient.ResponseType = srtXMLDocument) or (FSOAPClient.ResponseType = srtSOAPMessage);
    UpdateResponse;
  except
    on E : Exception do
    begin
      MessageDlg('Failed to send SOAP message: ' + E.Message, mtError, [mbOk], 0);
    end;
  end;

  hpXml.Free;
  hpXMLDocument.Free;
  hpStream.Free;
end;


File test.xml contains prepared soap envelope. After sending message by FSOAPClient.SendMessage; the sent message contains <?xml version="1.0" encoding="UTF-16"?> instead of <?xml version="1.0" encoding="UTF-8"?>, which I need.


[ Download ]
#37561
Posted: 08/25/2016 09:23:56
by Dmytro Bogatskyy (EldoS Corp.)

Hi,

Quote
Here is source code (derived from sample):

The code above should send xml document in UTF-8 encoding, and setting Encoding property is not necessary as UTF-8 encoding is used by default.
Quote
After sending message by FSOAPClient.SendMessage; the sent message contains <?xml version="1.0" encoding="UTF-16"?> instead of <?xml version="1.0" encoding="UTF-8"?>, which I need.

How do you check the encoding of the sent message? Do you intercept the data that sent to a server or you check the data received by the server?
#37564
Posted: 08/25/2016 12:57:01
by Marek Dostal (Premium support level)
Joined: 08/11/2016
Posts: 5

I see it by clicking to button btnExploreRequestXML and btnExploreResponseXML in sample. In request and in response is "UTF-16".
And in response from web service is error "bad format of xml".
If I send the same soap message by another way (by Indy components)
to the web service with "UTF-8", web service accept the message and with "UTF-16", web service returns this error "bad format of xml".
We must use yours component, because web service requires TLS 1.2 and on WinXP this protocol is missing and Indy components don't implement TSL.
#37565
Posted: 08/25/2016 13:57:59
by Eugene Mayevski (EldoS Corp.)

Quote
Marek Dostal wrote:
I see it by clicking to button btnExploreRequestXML and btnExploreResponseXML in sample. In request and in response is "UTF-16".


Export of XML from the XML DOM is done in UTF-16 by default, yet this has nothing to do with how the message is actually sent.

Quote
Marek Dostal wrote:
If I send the same soap message by another way (by Indy components) to the web service with "UTF-8", web service accept the message and with "UTF-16", web service returns this error "bad format of xml".


Unfortunately from your message it's not clear, what happens in case of Indy. Is it the same error? Then maybe the problem is not with Unicode at all?


Sincerely yours
Eugene Mayevski
#37566
Posted: 08/25/2016 14:14:16
by Dmytro Bogatskyy (EldoS Corp.)

Hi,

Quote
I see it by clicking to button btnExploreRequestXML and btnExploreResponseXML in sample. In request and in response is "UTF-16".

In fact, those buttons shows a form with not a real request/response, but with parsed XML tree. The edit box on this form shows the xml content of the selected node, it uses TElXMLDOMNode.OuterXML property for this. This property returns unicode (UTF-16) string, that's why you see "UTF-16" encoding there.
Quote
And in response from web service is error "bad format of xml".

Please try to set TElXMLSOAPClient.SOAPVersion property to SOAP_v1_1 value, as your file contain SOAP message version 1.1
Also, you may need to set TElXMLSOAPClient.SOAPAction property. If you omit this property then SOAPAction HTTP request header will not be included in the request.
#37568
Posted: 08/26/2016 01:32:50
by Marek Dostal (Premium support level)
Joined: 08/11/2016
Posts: 5

I thought, that by this buttons in example I see real request. So, I wrote simple webservice to see, what I received. And the problem is: data contains not only my soap message, but also header like this:
Code
POST / HTTP/1.1
POST / HTTP/1.1Content-Type: text/xml; charset=UTF-8
Host: 127.0.0.1:9100
User-Agent: SecureBlackbox
Accept-Encoding: gzip, deflate
Connection: Close
Content-Length: 4504

and web service "https://pg.eet.cz:443/eet/services/EETServiceSOAP/v3" don't accept this. It must be only soap message.
Please, is possible to send soap message without this header?
#37572
Posted: 08/26/2016 07:21:51
by Dmytro Bogatskyy (EldoS Corp.)

Hi,

Quote
And the problem is: data contains not only my soap message, but also header like this:
Code
POST / HTTP/1.1
POST / HTTP/1.1Content-Type: text/xml; charset=UTF-8

Are there a typo in the data that you have posted or the header really contains "POST / HTTP/1.1" string twice?
Quote
Please, is possible to send soap message without this header?

In this case, you can use TElSimpleSSLClient component to send data (SOAP message) directly to a server. Please refer to the sample at \EldoS\SecureBlackbox.VCL\Samples\Delphi\SSLBlackbox\Client\SimpleSSL folder after SecureBlackbox installation.
#37574
Posted: 08/26/2016 08:03:54
by Marek Dostal (Premium support level)
Joined: 08/11/2016
Posts: 5

Thanks for answer.

Quote

Are there a typo in the data that you have posted or the header really contains "POST / HTTP/1.1" string twice?

Yes, there is really this part twice.

I'll try to use sample SimpleSSL.
Also by EldoS: MsgConnect
Cross-platform protocol-independent communication framework for building peer-to-peer and client-server applications and middleware components.

Reply

Statistics

Topic viewed 1210 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!