02-01-2013 05:31 AM
I required to add a soap header to the soap request. I am using qtsoap to post soap request. This is my complete request. How can I add headers in code...!
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> <SecureSoapHeader xmlns="http://tempuri.org/"> <Platform>string</Platform> <AuthToken>string</AuthToken> </SecureSoapHeader> </soap:Header> <soap:Body> <GetCitiesByCountryCode xmlns="http://tempuri.org/"> <countryCode>string</countryCode> </GetCitiesByCountryCode> </soap:Body> </soap:Envelope>
I tried like this:
QtSoapMessage request; QtSoapStruct soap_struct(QtSoapQName("SecureSoapHeader", "http://tempuri.org/")); soap_struct.insert(new QtSoapSimpleType(QtSoapQName("Platform"), "BlackBerry")); soap_struct.insert(new QtSoapSimpleType(QtSoapQName("AuthToken"), "4A2EDCDA-E5A9-4E77-B41A-F1C1FF15A645")); request.addHeaderItem(&soap_struct); request.setMethod("GetCitiesByCountryCode", "http://tempuri.org/"); request.addMethodArgument("countryCode", "", "IN"); http.setHost("dev4.winitsoftware.com"); http.setAction("http://tempuri.org/GetCitiesByCountryCode"); http.submitRequest(request, "/EzeegoMiddleware/Services/HotelService.asmx");
After specifying this the application is closing...
If any one tried this, help me please!
Solved! Go to Solution.
02-01-2013 05:44 AM - edited 02-01-2013 05:51 AM
Hi! I did not use this API but I assume submitRequest is asynchronous.
You are creating soap_struct on the stack (so it's a local variable).
After submitRequest method is called the function will terminate and all local variables will be destroyed, so the address passed to addHeaderItem will become invalid.
Try creating soap_struct on the heap:
QtSoapStruct *soap_struct = new QtSoapStruct(...) soap_struct->insert(...); request.addHeaderItem(soap_struct);
02-01-2013 06:12 AM
Local variables (created on stack) will be destroyed automatically after exit from a block (or function) in which they were created. This is convenient for temporary objects, but be careful not to pass an address of such object to another object which can try to use it later.
When creating objects on heap they must be destroyed explicitly otherwise memory leaks will occur:
MyObject *o = new MyObject;
But in Qt it's usually best to create objects on heap, but set another object as their parent so they will be destroyed automatically when their parent is destroyed:
SomeWidget *w = new SomeWidget(this); // set 'this' as widget's parent. When 'this' is deleted, 'w' will be deleted too
setParent method does the same.
Object must be a QObject descendent for this to work.
In the case above I don't see the parent property, but most likely QtSoapMessage will take ownership of the object and will destroy it later although docs don't mention that...
02-02-2013 12:36 AM
Its great to know about the memory management techniques..! If I create more no of objects in the heap in a method, then should I set their parent as the present class object so that they will be deleted with the class object?
Is there any way to clear the memory blocks occupied by those memory blocks....?
And onething I am not able to capture is, the exact use of class's destructor....! can you give a link for this concept..!