有的时候gsoap调用别人提供的接口,需要认证才能进去。
1.首先是需要用户名和密码,可以如下指定:
struct soap info_soap;
soap_init(&info_soap);
info_soap.userid = "superadmin";
info_soap.passwd = "123456";
2.需要下载对应的证书,可以用火狐下载数字证书,修改名字放在对应的开发目录下面,以供调用,然后在程序中指明证书的名字,如下:
soap_ssl_client_context(&info_soap, 0, NULL, NULL, "/opt/create_user/sipXCert.pem", "/opt/create_user/", NULL);
注意:如果要下载指定的wsdl文件,要到指定的说明文档复制对应下载链接,替换对应服务器的IP地址:https://yourdomain:8443/sipxconfig/services/*Service
打开这个网址的时候会提示你证书的问题,然后下载该证书就可以了。
在linux下可以直接输入命令下载这个wsdl:wget --no-check-certificate URL --http-user=superadmin --http-password=123456
可以参看下面的完整代码:
#include <iostream>
#include <string>
#include "UserServiceSoapBinding.nsmap"
#include "soapH.h"
int sendWebserviceItem(
const char* url,
const char* superadminPin,
const char* userName
)
{
int res = -1;
struct soap info_soap;
soap_init(&info_soap);
info_soap.userid = "superadmin";
info_soap.passwd = "123456";
//info_soap.proxy_userid = "superadmin";
//info_soap.proxy_passwd = "123456";
info_soap.send_timeout = 3;
info_soap.recv_timeout = 3;
soap_set_mode (&info_soap, SOAP_C_UTFSTRING);
#ifdef DEBUG
soap_set_test_logfile (&info_soap, "TEST.log");
soap_set_test_logfile (&info_soap, "RECV.log");
soap_set_test_logfile (&info_soap, "SEND.log");
#endif
#ifdef _PROXY_IP
_soap.proxy_host = _PROXY_IP;
#endif
#ifdef _PROXY_PORT
_soap.proxy_port = _PROXY_PORT;
#endif
ns1__AddUser preq;
struct __ns1__addUserResponse pres;
preq.pin = superadminPin;
preq.user = new ns1__User;
if (preq.user == NULL)
{
std::cout << "preq.user new memory failed" << std::endl;
soap_print_fault(&info_soap, stderr);
#ifdef DEBUG
soap_close_logfiles (&info_soap);
#endif
soap_cleanup(&info_soap);
soap_destroy (&info_soap);
soap_end (&info_soap);
soap_done (&info_soap);
return -1;
}
std::string strUserName = userName;
preq.user->userName = strUserName;
soap_ssl_client_context(&info_soap, 0, NULL, NULL, "/opt/create_user/sipXCert.pem", "/opt/create_user/", NULL);
//info_soap.authrealm = SOAP_AUTHENTICATION_DIGEST;
res = soap_call___ns1__addUser(&info_soap, url, "", &preq, pres);
if (res != 0)
{
std::cout << "soap_call___ns1__addUser error:" << res << " soap.error:" << info_soap.error << std::endl;
#ifdef DEBUG
soap_close_logfiles (&info_soap);
#endif
delete preq.user;
preq.user = NULL;
soap_cleanup(&info_soap);
soap_destroy (&info_soap);
soap_end (&info_soap);
soap_done (&info_soap);
return -1;
}
#ifdef DEBUG
soap_close_logfiles (&info_soap);
#endif
delete preq.user;
preq.user = NULL;
soap_cleanup(&info_soap);
soap_destroy (&info_soap);
soap_end (&info_soap);
soap_done (&info_soap);
return 0;
}
int main(int argc, char* argv[])
{
if (argc != 4)
{
std::cout << "arg error" << std::endl;
return -1;
}
int result = sendWebserviceItem(argv[1], argv[2], argv[3]);
if (result != 0)
return -1;
return 0;
}