gSoap how can I read a set of data from web services using credentials

167 Views Asked by At

I'm new to gSoap and need to create a web service client that receives a response containing several records in C.

Through wsdl2h and soapcpp2 I generated the various intermediate files, below soapStub.h

#ifndef soapStub_H
#define soapStub_H
#define SOAP_NAMESPACE_OF_ns1   "urn:eda-sw-schemas/page/salesorder"
#include "stdsoap2.h"
#ifdef __cplusplus
extern "C" {
#endif

/******************************************************************************\
 *                                                                            *
 * Enumerations                                                               *
 *                                                                            *
\******************************************************************************/


#ifndef SOAP_TYPE_xsd__boolean
#define SOAP_TYPE_xsd__boolean (7)
/* xsd:boolean */
enum xsd__boolean {xsd__boolean__false_ = 0, xsd__boolean__true_ = 1};
#endif

#ifndef SOAP_TYPE_ns1__Job_USCOREQueue_USCOREStatus
#define SOAP_TYPE_ns1__Job_USCOREQueue_USCOREStatus (10)
/* ns1:Job_Queue_Status */
enum ns1__Job_USCOREQueue_USCOREStatus {ns1__Job_USCOREQueue_USCOREStatus___USCOREblank_USCORE = 0, ns1__Job_USCOREQueue_USCOREStatus__Scheduled_USCOREfor_USCOREPosting = 1, ns1__Job_USCOREQueue_USCOREStatus__Error = 2, ns1__Job_USCOREQueue_USCOREStatus__Posting = 3};
#endif

#ifndef SOAP_TYPE_ns1__Status
#define SOAP_TYPE_ns1__Status (11)
/* ns1:Status */
enum ns1__Status {ns1__Status__Open = 0, ns1__Status__Released = 1, ns1__Status__Pending_USCOREApproval = 2, ns1__Status__Pending_USCOREPrepayment = 3};
#endif

#ifndef SOAP_TYPE_ns1__Prepmt_USCORECM_USCORERefers_USCOREto_USCOREPeriod
#define SOAP_TYPE_ns1__Prepmt_USCORECM_USCORERefers_USCOREto_USCOREPeriod (12)
/* ns1:Prepmt_CM_Refers_to_Period */
enum ns1__Prepmt_USCORECM_USCORERefers_USCOREto_USCOREPeriod {ns1__Prepmt_USCORECM_USCORERefers_USCOREto_USCOREPeriod___USCOREblank_USCORE = 0, ns1__Prepmt_USCORECM_USCORERefers_USCOREto_USCOREPeriod__Current = 1, ns1__Prepmt_USCORECM_USCORERefers_USCOREto_USCOREPeriod__Current_USCORECalendar_USCOREYear = 2, ns1__Prepmt_USCORECM_USCORERefers_USCOREto_USCOREPeriod__Previous_USCORECalendar_USCOREYear = 3};
#endif

#ifndef SOAP_TYPE_ns1__Type
#define SOAP_TYPE_ns1__Type (13)
/* ns1:Type */
enum ns1__Type {ns1__Type___USCOREblank_USCORE = 0, ns1__Type__G_USCOREL_USCOREAccount = 1, ns1__Type__Item = 2, ns1__Type__Resource = 3, ns1__Type__Fixed_USCOREAsset = 4, ns1__Type__Charge_USCOREItem = 5};
#endif

#ifndef SOAP_TYPE_ns1__IC_USCOREPartner_USCORERef_USCOREType
#define SOAP_TYPE_ns1__IC_USCOREPartner_USCORERef_USCOREType (14)
/* ns1:IC_Partner_Ref_Type */
enum ns1__IC_USCOREPartner_USCORERef_USCOREType {ns1__IC_USCOREPartner_USCORERef_USCOREType___USCOREblank_USCORE = 0, ns1__IC_USCOREPartner_USCORERef_USCOREType__G_USCOREL_USCOREAccount = 1, ns1__IC_USCOREPartner_USCORERef_USCOREType__Item = 2, ns1__IC_USCOREPartner_USCORERef_USCOREType__Charge_USCOREItem = 3, ns1__IC_USCOREPartner_USCORERef_USCOREType__Cross_USCOREReference = 4, ns1__IC_USCOREPartner_USCORERef_USCOREType__Common_USCOREItem_USCORENo = 5, ns1__IC_USCOREPartner_USCORERef_USCOREType__Vendor_USCOREItem_USCORENo = 6};
#endif

#ifndef SOAP_TYPE_ns1__Prepmt_USCORECM_USCORERefers_USCOREto_USCOREPeriod_USCORE12139
#define SOAP_TYPE_ns1__Prepmt_USCORECM_USCORERefers_USCOREto_USCOREPeriod_USCORE12139 (15)
/* ns1:Prepmt_CM_Refers_to_Period_12139 */
enum ns1__Prepmt_USCORECM_USCORERefers_USCOREto_USCOREPeriod_USCORE12139 {ns1__Prepmt_USCORECM_USCORERefers_USCOREto_USCOREPeriod_USCORE12139___USCOREblank_USCORE = 0, ns1__Prepmt_USCORECM_USCORERefers_USCOREto_USCOREPeriod_USCORE12139__Current = 1, ns1__Prepmt_USCORECM_USCORERefers_USCOREto_USCOREPeriod_USCORE12139__Current_USCORECalendar_USCOREYear = 2, ns1__Prepmt_USCORECM_USCORERefers_USCOREto_USCOREPeriod_USCORE12139__Previous_USCORECalendar_USCOREYear = 3};
#endif

#ifndef SOAP_TYPE_ns1__SalesOrder_USCOREFields
#define SOAP_TYPE_ns1__SalesOrder_USCOREFields (16)
/* ns1:SalesOrder_Fields */
enum ns1__SalesOrder_USCOREFields {ns1__SalesOrder_USCOREFields__No = 0, ns1__SalesOrder_USCOREFields__Sell_USCOREto_USCORECustomer_USCORENo = 1, ns1__SalesOrder_USCOREFields__Sell_USCOREto_USCORECustomer_USCOREName = 2, ns1__SalesOrder_USCOREFields__Job_USCOREQueue_USCOREStatus = 3, ns1__SalesOrder_USCOREFields__Status = 4};
#endif

/******************************************************************************\
 *                                                                            *
 * Types with Custom Serializers                                              *
 *                                                                            *
\******************************************************************************/


/******************************************************************************\
 *                                                                            *
 * Classes and Structs                                                        *
 *                                                                            *
\******************************************************************************/


#if 0 /* volatile type: do not declare here, declared elsewhere */

#endif

#ifndef SOAP_TYPE_ns1__Sales_USCOREOrder_USCORELine
#define SOAP_TYPE_ns1__Sales_USCOREOrder_USCORELine (17)
/* ns1:Sales_Order_Line */
struct ns1__Sales_USCOREOrder_USCORELine
{
    char *Key;  /* optional element of type xsd:string */
    enum ns1__Type *Type;   /* optional element of type ns1:Type */
    char *FilteredTypeField;    /* optional element of type xsd:string */
    char *No;   /* optional element of type xsd:string */
    enum xsd__boolean *Include_USCOREin_USCOREVAT_USCORETransac_USCORERep;  /* optional element of type xsd:boolean */
    enum ns1__IC_USCOREPartner_USCORERef_USCOREType *IC_USCOREPartner_USCORERef_USCOREType; /* optional element of type ns1:IC_Partner_Ref_Type */
    enum ns1__Prepmt_USCORECM_USCORERefers_USCOREto_USCOREPeriod_USCORE12139 *Prepmt_USCORECM_USCORERefers_USCOREto_USCOREPeriod;   /* optional element of type ns1:Prepmt_CM_Refers_to_Period_12139 */
    char *Requested_USCOREDelivery_USCOREDate;  /* optional element of type xsd:date */
    char *Whse_USCOREOutstanding_USCOREQty; /* optional element of type xsd:decimal */
    int *Appl_USCOREfrom_USCOREItem_USCOREEntry;    /* optional element of type xsd:int */
};
#endif

#ifndef SOAP_TYPE_ns1__Sales_USCOREOrder_USCORELine_USCOREList
#define SOAP_TYPE_ns1__Sales_USCOREOrder_USCORELine_USCOREList (23)
/* ns1:Sales_Order_Line_List */
struct ns1__Sales_USCOREOrder_USCORELine_USCOREList
{
    int __sizeSales_USCOREOrder_USCORELine; /* sequence of elements <Sales_Order_Line> */
    struct ns1__Sales_USCOREOrder_USCORELine *Sales_USCOREOrder_USCORELine; /* required element of type ns1:Sales_Order_Line */
};
#endif

#ifndef SOAP_TYPE_ns1__SalesOrder
#define SOAP_TYPE_ns1__SalesOrder (25)
/* ns1:SalesOrder */
struct ns1__SalesOrder
{
    char *Key;  /* optional element of type xsd:string */
    char *No;   /* optional element of type xsd:string */
    char *Sell_USCOREto_USCORECustomer_USCORENo;    /* optional element of type xsd:string */
    char *Sell_USCOREto_USCORECustomer_USCOREName;  /* optional element of type xsd:string */
    enum ns1__Job_USCOREQueue_USCOREStatus *Job_USCOREQueue_USCOREStatus;   /* optional element of type ns1:Job_Queue_Status */
    enum ns1__Status *Status;   /* optional element of type ns1:Status */
    struct ns1__Sales_USCOREOrder_USCORELine_USCOREList *SalesLines;    /* optional element of type ns1:Sales_Order_Line_List */
};
#endif

#ifndef SOAP_TYPE_ns1__SalesOrder_USCOREList
#define SOAP_TYPE_ns1__SalesOrder_USCOREList (29)
/* ns1:SalesOrder_List */
struct ns1__SalesOrder_USCOREList
{
    int __sizeSalesOrder;   /* sequence of elements <SalesOrder> */
    struct ns1__SalesOrder *SalesOrder; /* required element of type ns1:SalesOrder */
};
#endif

#ifndef SOAP_TYPE_ns1__SalesOrder_USCOREFilter
#define SOAP_TYPE_ns1__SalesOrder_USCOREFilter (31)
/* ns1:SalesOrder_Filter */
struct ns1__SalesOrder_USCOREFilter
{
    enum ns1__SalesOrder_USCOREFields Field;    /* required element of type ns1:SalesOrder_Fields */
    char *Criteria; /* required element of type xsd:string */
};
#endif

#ifndef SOAP_TYPE__ns1__ReadMultiple
#define SOAP_TYPE__ns1__ReadMultiple (32)
/* ns1:ReadMultiple */
struct _ns1__ReadMultiple
{
    int __sizefilter;   /* sequence of elements <filter> */
    struct ns1__SalesOrder_USCOREFilter *filter;    /* required element of type ns1:SalesOrder_Filter */
    char *bookmarkKey;  /* optional element of type xsd:string */
    int setSize;    /* required element of type xsd:int */
};
#endif

#ifndef SOAP_TYPE__ns1__ReadMultiple_USCOREResult
#define SOAP_TYPE__ns1__ReadMultiple_USCOREResult (34)
/* ns1:ReadMultiple_Result */
struct _ns1__ReadMultiple_USCOREResult
{
    struct ns1__SalesOrder_USCOREList *ReadMultiple_USCOREResult;   /* SOAP 1.2 RPC return element (when namespace qualified) */    /* optional element of type ns1:SalesOrder_List */
};
#endif

#ifndef SOAP_TYPE___ns1__ReadMultiple
#define SOAP_TYPE___ns1__ReadMultiple (39)
/* Operation wrapper: */
struct __ns1__ReadMultiple
{
    struct _ns1__ReadMultiple *ns1__ReadMultiple;   /* optional element of type ns1:ReadMultiple */
};
#endif

#ifndef SOAP_TYPE_SOAP_ENV__Header
#define SOAP_TYPE_SOAP_ENV__Header (40)
/* SOAP Header: */
struct SOAP_ENV__Header
{
#ifdef WITH_NOEMPTYSTRUCT
    char dummy; /* dummy member to enable compilation */
#endif
};
#endif

#ifndef SOAP_TYPE_SOAP_ENV__Code
#define SOAP_TYPE_SOAP_ENV__Code (41)
/* SOAP Fault Code: */
struct SOAP_ENV__Code
{
    char *SOAP_ENV__Value;  /* optional element of type xsd:QName */
    struct SOAP_ENV__Code *SOAP_ENV__Subcode;   /* optional element of type SOAP-ENV:Code */
};
#endif

#ifndef SOAP_TYPE_SOAP_ENV__Detail
#define SOAP_TYPE_SOAP_ENV__Detail (43)
/* SOAP-ENV:Detail */
struct SOAP_ENV__Detail
{
    int __type; /* any type of element <fault> (defined below) */
    void *fault;    /* transient */
    char *__any;
};
#endif

#ifndef SOAP_TYPE_SOAP_ENV__Reason
#define SOAP_TYPE_SOAP_ENV__Reason (46)
/* SOAP-ENV:Reason */
struct SOAP_ENV__Reason
{
    char *SOAP_ENV__Text;   /* optional element of type xsd:string */
};
#endif

#ifndef SOAP_TYPE_SOAP_ENV__Fault
#define SOAP_TYPE_SOAP_ENV__Fault (47)
/* SOAP Fault: */
struct SOAP_ENV__Fault
{
    char *faultcode;    /* optional element of type xsd:QName */
    char *faultstring;  /* optional element of type xsd:string */
    char *faultactor;   /* optional element of type xsd:string */
    struct SOAP_ENV__Detail *detail;    /* optional element of type SOAP-ENV:Detail */
    struct SOAP_ENV__Code *SOAP_ENV__Code;  /* optional element of type SOAP-ENV:Code */
    struct SOAP_ENV__Reason *SOAP_ENV__Reason;  /* optional element of type SOAP-ENV:Reason */
    char *SOAP_ENV__Node;   /* optional element of type xsd:string */
    char *SOAP_ENV__Role;   /* optional element of type xsd:string */
    struct SOAP_ENV__Detail *SOAP_ENV__Detail;  /* optional element of type SOAP-ENV:Detail */
};
#endif

/******************************************************************************\
 *                                                                            *
 * Typedefs                                                                   *
 *                                                                            *
\******************************************************************************/

#ifndef SOAP_TYPE__QName
#define SOAP_TYPE__QName (5)
typedef char *_QName;
#endif

#ifndef SOAP_TYPE__XML
#define SOAP_TYPE__XML (6)
typedef char *_XML;
#endif

#ifndef SOAP_TYPE_xsd__date
#define SOAP_TYPE_xsd__date (8)
typedef char *xsd__date;
#endif

#ifndef SOAP_TYPE_xsd__decimal
#define SOAP_TYPE_xsd__decimal (9)
typedef char *xsd__decimal;
#endif


/******************************************************************************\
 *                                                                            *
 * Externals                                                                  *
 *                                                                            *
\******************************************************************************/


/******************************************************************************\
 *                                                                            *
 * Server-Side Operations                                                     *
 *                                                                            *
\******************************************************************************/


SOAP_FMAC5 int SOAP_FMAC6 __ns1__ReadMultiple(struct soap*, struct _ns1__ReadMultiple *ns1__ReadMultiple, struct _ns1__ReadMultiple_USCOREResult *ns1__ReadMultiple_USCOREResult);

/******************************************************************************\
 *                                                                            *
 * Server-Side Skeletons to Invoke Service Operations                         *
 *                                                                            *
\******************************************************************************/

SOAP_FMAC5 int SOAP_FMAC6 soap_serve(struct soap*);

SOAP_FMAC5 int SOAP_FMAC6 soap_serve_request(struct soap*);

SOAP_FMAC5 int SOAP_FMAC6 soap_serve___ns1__ReadMultiple(struct soap*);

/******************************************************************************\
 *                                                                            *
 * Client-Side Call Stubs                                                     *
 *                                                                            *
\******************************************************************************/


SOAP_FMAC5 int SOAP_FMAC6 soap_call___ns1__ReadMultiple(struct soap *soap, const char *soap_endpoint, const char *soap_action, struct _ns1__ReadMultiple *ns1__ReadMultiple, struct _ns1__ReadMultiple_USCOREResult *ns1__ReadMultiple_USCOREResult);

#ifdef __cplusplus
}
#endif

#endif

/* End of soapStub.h */

In SalesOrderRedux.c file I implemented the following code:

#include "soapH.h"
#include "SalesOrder_USCOREBinding.nsmap"
int main() { 
    struct soap soap;
    struct _ns1__ReadMultiple_USCOREResult *ns1__ReadMultiple_USCOREResult;
    soap_init(&soap);
    soap.userid = "administrator";
    soap.passwd = "password";
    
    soap_call___ns1__ReadMultiple(&soap, "http://localhost:8012/ERP/WS/Vendor/Page/SalesOrder", "", NULL, ns1__ReadMultiple_USCOREResult);
    
    printf("ns1__ReadMultiple_USCOREResult %s\n", (ns1__ReadMultiple_USCOREResult == NULL)?"is NULL":"is Not NULL");
    
    return 0; 
}

The above program is a draft of what I'm trying to do: How can I connect to the web service via credentials and getting the dataset from the soap_call___ns1__ReadMultiple service?

Update I followed rveerd's suggestion :

I have the message:

Error 401 fault: SOAP-ENV:Server [no subcode] 
"HTTP Error" Detail: HTTP/1.1 401 Unauthorized

If I connect to the url localhost:8012/ERP/WS/Vendor/Page/SalesOrder using a browser, the panel for entering the credentials is shown to me, I enter them and the wsdl layout is shown.

If i use the following powershell script the connection works:

$username = "myUser"
$password = "MyPasswd"

$URI = "http://localhost:8012/ERP/WS/Vendor/Page/SalesOrder"
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
$WSProxy = New-WebServiceProxy -Uri $URI -Credential $cred
$WSProxy.ReadMultiple($null, "", 0)

All tests are performed locally on the same machine

What I'm wrong ?

1

There are 1 best solutions below

3
rveerd On

The gSoap website has a basic client in C. Changing it for your example gives:

int main()
{
  struct soap *soap = soap_new(); // allocate and initialize a context
  soap->userid = "administrator";
  soap->passwd = "password";
  struct _ns1__ReadMultiple_USCOREResult res;
  if (soap_call___ns1__ReadMultiple(soap, "http://localhost:8012/ERP/WS/Vendor/Page/SalesOrder", "", NULL, &res) == SOAP_OK) 
  {
    // process result through `res`
  }
  else
  {
    soap_print_fault(soap, stderr);
  }
  soap_destroy(soap); // delete managed deserialized C++ instances
  soap_end(soap);     // delete other managed data
  soap_free(soap);    // free the soap struct context data
}

Setting userid and passwd only works when the web service uses HTTP basic authentication.

Printing the error when the SOAP request fails hopefully gives you some idea about what goes wrong.

You can also build gSOAP in debug mode by defining the macro DEBUG.