您的位置:首页 > 编程语言 > C语言/C++

C++调用java基于XFire的WebService接口

2014-04-04 16:41 501 查看
1、修改soapStub.h文件的结构体SOAP_ENV__Header
/* SOAP Header: */
struct SOAP_ENV__Header
{
#ifdef WITH_NOEMPTYSTRUCT
private:
char dummy;	/* dummy member to enable compilation */
public:
int mustUnderstand;
char *wsse__username;
char *wsse__password;

SOAP_ENV__Header(void)
{
mustUnderstand = 1;
wsse__username = "username";
wsse__password = "";
}
#endif
};

2、修改soapC.cpp文件

SOAP_FMAC3 int SOAP_FMAC4 soap_out_SOAP_ENV__Header(struct soap *soap, const char *tag, int id, const struct SOAP_ENV__Header *a, const char *type)
{
/*(void)soap; (void)tag; (void)id; (void)type;
if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, a, SOAP_TYPE_SOAP_ENV__Header), type))
return soap->error;
return soap_element_end_out(soap, tag);*/

(void)soap; (void)tag; (void)id; (void)type;
if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, a, SOAP_TYPE_SOAP_ENV__Header), type))
return soap->error;
if (soap_element_begin_out(soap, "wsse:Security", -1, "")
|| soap_element_begin_out(soap, "wsse:UsernameToken", -1, "")
|| soap_out_string(soap, "wsse:Username", -1, &a->wsse__username, "")
|| soap_out_string(soap, "wsse:Password", -1, &a->wsse__password, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText")
|| soap_element_end_out(soap, "wsse:UsernameToken")
|| soap_element_end_out(soap, "wsse:Security")
)
return soap->error;
soap->mustUnderstand = 1;
return soap_element_end_out(soap, tag);
}
// 此处,若返回文件无需Header头验证,可不修改
SOAP_FMAC3 struct SOAP_ENV__Header * SOAP_FMAC4 soap_in_SOAP_ENV__Header(struct soap *soap, const char *tag, struct SOAP_ENV__Header *a, const char *type)
{
struct SOAP_ENV__Header header;
header.wsse__username = "IUTEIt1Frec=";
soap->header = &header;
soap->mustUnderstand = 1;
if (soap_element_begin_in(soap, tag, 0, type))
return NULL;
a = (struct SOAP_ENV__Header *)soap_id_enter(soap, soap->id, a, SOAP_TYPE_SOAP_ENV__Header, sizeof(struct SOAP_ENV__Header), 0, NULL, NULL, NULL);
if (!a)
return NULL;
soap_default_SOAP_ENV__Header(soap, a);

size_t soap_flag_wsse__security = 1;
if (soap->body && !*soap->href)
{
for (;;)
{
if ( soap_flag_wsse__security ) {
if ( soap_element_begin_in(soap, NULL, 0, NULL) )
return NULL;
if ( soap_element_begin_in(soap, NULL, 0, NULL) )
return NULL;
if ( soap_in_string(soap, "", &a->wsse__username, "")
&& soap_in_string(soap, "", &a->wsse__password, "") ) {
soap_flag_wsse__security--;
soap_element_end_in(soap, NULL);
soap_element_end_in(soap, NULL);
}
}

soap->error = SOAP_TAG_MISMATCH;
if (soap->error == SOAP_TAG_MISMATCH)
soap->error = soap_ignore_element(soap);
if (soap->error == SOAP_NO_TAG)
break;
if (soap->error)
return NULL;
}
if (soap_element_end_in(soap, tag))
return NULL;
}
else
{	a = (struct SOAP_ENV__Header *)soap_id_forward(soap, soap->href, (void*)a, 0, SOAP_TYPE_SOAP_ENV__Header, 0, sizeof(struct SOAP_ENV__Header), 0, NULL);
if (soap->body && soap_element_end_in(soap, tag))
return NULL;
}
return a;
}
3、修改NetImageServiceHttpBindingProxy.cpp文件

void NetImageServiceHttpBindingProxy::NetImageServiceHttpBindingProxy_init(soap_mode imode, soap_mode omode)
{	soap_imode(this->soap, imode);
soap_omode(this->soap, omode);
soap_endpoint = NULL;
static const struct Namespace namespaces[] =
{
{"SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/", "http://www.w3.org/*/soap-envelope", NULL},
{"SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/", "http://www.w3.org/*/soap-encoding", NULL},
{"xsi", "http://www.w3.org/2001/XMLSchema-instance", "http://www.w3.org/*/XMLSchema-instance", NULL},
{"xsd", "http://www.w3.org/2001/XMLSchema", "http://www.w3.org/*/XMLSchema", NULL},
{"ns1", "http://service.meiah.com", NULL, NULL},
{"ns2", "http://po.meiah.com", NULL, NULL},
{"ns3", "http://pojo.sys.meiya.com", NULL, NULL},
{"ns4", "http://pojo.forensics.meiya.com", NULL, NULL},
{"ns5", "http://pojo.clue.meiya.com", NULL, NULL},
{"wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", NULL, NULL},
{NULL, NULL, NULL, NULL}
};
soap_set_namespaces(this->soap, namespaces);
}


在namespaces中添加{"wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", NULL, NULL}

4、调用接口

struct SOAP_ENV__Header header;
header.wsse__username = "IUTEIt1Frec=";
header.wsse__password = "";
NetImageServiceHttpBindingProxy::soap->header = &header;
soap_set_mode(NetImageServiceHttpBindingProxy::soap, SOAP_C_UTFSTRING); // 中文乱码问题
int nRet = NetImageServiceHttpBindingProxy::checkUser(&input, &output);

注:若是https://的接口,需要key,可加上下面几句:

     

NetImageServiceHttpBindingProxy::soap->ssl_flags = SOAP_SSL_NO_AUTHENTICATION;
NetImageServiceHttpBindingProxy::soap->keyfile = "cert.pem";
NetImageServiceHttpBindingProxy::soap->password = "123456"; // 看需要


在调试时可通过Wireshark进行抓包分析,客户端发送的xml格式为:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:ns2="http://po.meiah.com"
xmlns:ns3="http://pojo.sys.meiya.com"
xmlns:ns4="http://pojo.forensics.meiya.com"
xmlns:ns5="http://pojo.clue.meiya.com"
xmlns:ns1="http://service.meiah.com">
<SOAP-ENV:Header>
<wsse:Security SOAP-ENV:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<UsernameToken>
<Username>IUTEIt1Frec=</Username>
</UsernameToken>
</wsse:Security>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:checkUser>
<ns1:in0>
<ns3:password></ns3:password>
<ns3:username></ns3:username>
</ns1:in0>
</ns1:checkUser>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>


服务端返回值

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ns1:checkUserResponse>
<ns1:out>验证成功</ns1:out>
</ns1:checkUserResponse>
</soap:Body>
</soap:Envelope>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  xfire java c++ webservice