一个简单的使用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;
}
#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;
}
相关文章推荐
- 使用Socket模拟一个简单的Webservice调用
- 【远程调用框架】如何实现一个简单的RPC框架(二)实现与使用
- 使用Socket模拟一个简单的Webservice调用
- 使用CXF框架实现webservice的一个简单例子
- 使用Socket模拟一个简单的Webservice调用
- 使用Socket模拟一个简单的Webservice调用
- android之使用Soap协议调用webservice实现手机归属地查询
- 使用 java 实现一个简单的 markdown 语法解析器
- (14)树莓派B+使用L298N驱动控制四驱车并实现一个简单的web控制端
- 一个简单调用webservice的例子
- 使用hadoop命令rcc生成Record 一个简单的方法来实现自己的定义writable对象
- 一个简单的WebService实现例子,SOAP安全验证
- c++实现一个 调用多条输出(使用动态数组实现)
- 使用python实现一个简单的学生信息管理系统
- Vue初体验(七)使用Vue实现一个简单的聊天框
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
- 使用vs2005 webservice 创建,部署,调用的简单实例
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
- 使用Axis2实现WebService的发布和调用
- 使用R实现一个简单的连续系统模拟