A simple implementation of calling WebService using the SOAP protocol
# Include <stdio. h>
# Include <tchar. h>
# Include <windows. h>
# Include <iostream>
# Include <string>
# Pragma comment (Lib, "ws2_32.lib ")
# Define req_len 1024
# Define recv_len 1024
Inline bool check_param (char * X) {return (x = NULL )? True: false ;}
/*
HTTP 1.2
Here is a soap 1.2 request and response example. content which insight [...] need to be replaced!
-------- [Request] ---------
Post/imsg/testhelloworld. asmx HTTP/1.1
HOST: 192.168.0.196
Content-Type: Application/soap + XML; charset = UTF-8
Content-Length: [length]
<? XML version = "1.0" encoding = "UTF-8"?>
<Soap12: envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema" xmlns: soap12 = "http://www.w3.org/2003/05/soap-envelope">
<Soap12: Body>
<Helloworld xmlns = "http://tempuri.org/"/> -- the body we need to extract
</Soap12: Body>
</Soap12: envelope>
-------- [Response] --------
HTTP/1.1 200 OK
Content-Type: Application/soap + XML; charset = UTF-8
Content-Length: [length]
<? XML version = "1.0" encoding = "UTF-8"?>
<Soap12: envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema" xmlns: soap12 = "http://www.w3.org/2003/05/soap-envelope">
<Soap12: Body>
<Helloworldresponse xmlns = "http://tempuri.org/">
<Helloworldresult> [String] </Helloworldresponse>
</Soap12: Body>
</Soap12: envelope>
*/
// Usage as follow settig
//
// Host = "192.168.0.196"
// Wsname = "/imsg/testhelloworld. asmx HTTP/1.1"
// Wscontentbody = //
// Description:
// We can first log on the Web service location to see the call method (http: // 192.168.0.196/imsg/testhelloworld. asmx,
// And extract the soap body part, then we can call it on our program (Be sure it's a block call !)
//
// Written by: kejieleng
// Date: 2009-3-25
Bool webservicecall (char * Host, char * wsname, char * wscontentbody, STD: string & returnmsg)
{
Int ret;
// First connetct to the network
Socket S = socket (af_inet, sock_stream, ipproto_tcp );
If (S = socket_error)
Return false;
// Int timeout = 1000;
// Ret = setsockopt (S, sol_socket, so_sndtimeo, (char *) & timeout, sizeof (timeout ));
// If (ret = socket_error)
// Return 0;
Sockaddr_in SA;
SA. sin_family = af_inet;
SA. sin_port = htons (80 );
SA. sin_addr.s_addr = inet_addr (host );
Ret = connect (S, (const sockaddr *) (& SA), sizeof (SA ));
If (Ret! = No_error)
Return false;
// Initialize the httpheader
If (check_param (host ))
Return false;
STD: String strpostrequest;
// Must be capital
Strpostrequest = "Post ";
// WebService name
Check_param (wsname );
Strpostrequest + = wsname;
Strpostrequest + = "/N ";
// WebService host
Strpostrequest + = "Host :";
Strpostrequest + = host;
Strpostrequest + = "/N ";
// Content-Type
Strpostrequest + = "Content-Type: Application/soap + XML; charset = UTF-8/N ";
// Content-Length
Char Buf [4];
Zeromemory (BUF, 4 );
STD: String strsoaphead = "<? XML version =/"1.0/" encoding =/"UTF-8/"?> <Soap12: envelope xmlns: xsi =/"http://www.w3.org/2001/XMLSchema-instance/" xmlns: XSD =/"http://www.w3.org/2001/XMLSchema/" xmlns: soap12 =/"http://www.w3.org/2003/05/soap-envelope/"> <soap12: Body> ";
STD: String strsoaptail = "</soap12: Body> </soap12: envelope> ";
Sprintf (BUF, "% d", strlen (wscontentbody) + strsoaphead. Size () + strsoaptail. Size ());
Strpostrequest + = "Content-Length :";
Strpostrequest + = Buf;
Strpostrequest + = "/n ";
// Setup the WebService body
Strpostrequest + = strsoaphead;
// Say: If (check_param (wscontentbody ))
Return false;
Strpostrequest + = wscontentbody;
Strpostrequest + = strsoaptail;
// Send the WebService request to the host
Int Ss = 0;
Ss = Send (S, strpostrequest. c_str (), (INT) strpostrequest. Size (), null );
Char buffer [recv_len];
Zeromemory (buffer, recv_len );
Int I = recv_len;
// Recv
While (I = recv_len)
{
I = Recv (S, buffer, recv_len, null );
Returnmsg + = buffer;
}
Closesocket (s );
Return true;
}
Int _ tmain (INT argc, _ tchar * argv [])
{
Char * server_addr = "192.168.0.196 ";
// Use Winsock 2.2
Word version = makeword (2, 2 );
Wsadata data;
Int ret;
Ret = wsastartup (version, & data );
If (Ret! = 0)
Return 0;
Char * wsname = "/imsg/testhelloworld. asmx HTTP/1.1 ";
Char * wsbody = "
STD: String STR;
If (webservicecall (server_addr, wsname, wsbody, STR ))
STD: cout <Str. c_str () <STD: Endl;
// Closesocket (s );
Wsacleanup ();
Return 0;
}