Being able to create both a client and server so easily in C# is nice, but the use of C# is restricted to the Windows environment. For other environments, I might decide to use the csoap library. csoap is licensed under the LGPLv2 License, meaning we can link it to our programs without violating copyright. Using csoap, we are able to connect to a given Web Service, and communicate with it via SOAP. As I write this, the newest version of csoap is 1.1.0.
csoap depends on libxml2.
To install csoap version 1.1.0, first download libsoap-1.1.0.tar.gz to a temporary directory, e.g. /tmp/csoap
$ mkdir /tmp/csoap $ cd /tmp/csoap $ wget http://prdownloads.sourceforge.net/csoap/libsoap-1.1.0.tar.gz
$ tar -xf libsoap-1.1.0.tar.gz
$ cd /tmp/csoap/libsoap-1.1.0 $ ./configure --prefix=/usr $ make
# make install
Upon installation, csoap produces the following files:
csoap installs a config script csoap-config, that you can use to dynamically ask for commandline flags to pass to the compiler when using csoap.
$ csoap-config --version
1.1.0
$ csoap-config --prefix
/usr
$ csoap-config --libs
-L/usr/lib -lcsoap -lnanohttp -L/usr/lib -lxml2 -lz -lm
$ csoap-config --cflags
-I/usr/include/libcsoap-1.1 -I/usr/include/nanohttp-1.1 -I/usr/include/libxml2 -pthread
Sure, you could read the official documentation, but the examples given are full of typos. Do it my way, instead.
$ mkdir /tmp/client $ cd /tmp/client
HelloClient6 : HelloClient6.o gcc `csoap-config --libs` -o HelloClient6 HelloClient6.o HelloClient6.o : HelloClient6.c gcc `csoap-config --cflags` -c HelloClient6.c
#include <stdio.h> #include <libcsoap/soap-client.h> static char *url = "http://localhost:8000/HelloService"; static char *urn = "http://petio.org/2009/07/22/HelloService"; static char *method = "GetHello"; static char *action = "http://petio.org/2009/07/22/HelloService/HelloService/GetHello"; void err_soap(herror_t err); int main(int argc, char *argv[]) { herror_t err; SoapCtx *request; SoapCtx *response; /* ----------------------------------- */ /* Initialize SOAP Client */ /* ----------------------------------- */ err = soap_client_init_args(argc,argv); if(err!=H_OK) { err_soap(err); return 1; } /* ----------------------------------- */ /* Create "request" envelope */ /* ----------------------------------- */ err = soap_ctx_new_with_method(urn,method,&request); if(err!=H_OK) { err_soap(err); soap_client_destroy(); return 1; } /* ----------------------------------- */ /* Show "request" envelope */ /* ----------------------------------- */ printf("-------- Request --------\n"); xmlDocFormatDump(stdout, request->env->root->doc,1); printf("-------------------------\n"); /* ----------------------------------- */ /* Trade for "response" envelope */ /* ----------------------------------- */ err = soap_client_invoke(request,&response,url,action); if(err!=H_OK) { err_soap(err); soap_ctx_free(request); soap_client_destroy(); return 1; } /* ----------------------------------- */ /* Show "response" envelope */ /* ----------------------------------- */ printf("-------- Response --------\n"); xmlDocFormatDump(stdout, response->env->root->doc,1); printf("-------------------------\n"); /* ----------------------------------- */ /* Destroy SOAP Client */ /* ----------------------------------- */ soap_ctx_free(request); soap_ctx_free(response); soap_client_destroy(); return 0; } void err_soap(herror_t err) { if(err==H_OK) return; printf("%s():%s [%d]\n",herror_func(err), herror_message(err), herror_code(err)); herror_release(err); }
$ make
$ ls
Makefile HelloClient6 HelloClient6.c HelloClient6.o
$ ./HelloClient6
-------- Request --------
<?xml version="1.0"?>
<s:Envelope
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema"
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Header/>
<s:Body>
<m:GetHello xmlns:m="http://petio.org/2009/07/22/HelloService"> </m:GetHello>
</s:Body>
</s:Envelope>
-------------------------
-------- Response --------
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetHelloResponse xmlns="http://petio.org/2009/07/22/HelloService">
<GetHelloResult>hello</GetHelloResult>
</GetHelloResponse>
</s:Body>
</s:Envelope>
--------------------------
(The above XML has been formatted for this page)
I personally dislike outdated documentation. If anything here seems wrong, or perhaps did not work for you, please email me (jay@petio.org) and tell me so I can update it. Thanks!