您的位置:首页 > 其它

一个简单的使用SOAP协议调用Webservice实现

2009-03-25 16:45 761 查看
一个简单的使用SOAP协议调用Webservice实现

#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; }

/*
SOAP 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]</HelloWorldResult>
</HelloWorldResponse>
</soap12:Body>
</soap12:Envelope>
*/

// Usage as follow settig
//
// Host = "192.168.0.196"
// WSName = "/iMsg/TestHelloWorld.asmx HTTP/1.1"
// WSContentBody = <HelloWorld xmlns=/"http://tempuri.org//" />
//
// 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/n";

//Setup the webservice body

strPostRequest += strSOAPHead;
//say: <HelloWorld xmlns=/"http://tempuri.org//" />
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";

// 使用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 = "<HelloWorld xmlns=/"http://tempuri.org//" />";

std::string str;
if( WebServiceCall(server_addr,wsName,wsBody,str) )
std::cout<< str.c_str() << std:: endl;

//closesocket(s);
WSACleanup();

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: