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

本文提供了一个简单的C++代码示例,演示如何使用SOAP 1.2协议调用Webservice。通过创建HTTP POST请求,设置必要的头部信息,并发送XML格式的SOAP消息来与远程服务交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个简单的使用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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值