如何在C中访问SOAP标头

时间:2012-11-12 13:51:56

标签: c++ c onvif

我是SOAP的新手,我正在尝试实现用户身份验证,我请求帮助,

以下是从客户端发送的XML请求

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:tt="http://www.onvif.org/ver10/schema"
            xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
            xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
            xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
    <soapenv:Header>
              <wsse:Security>
                <wsse:UsernameToken>
                  <wsse:Username>username</wsse:Username>

        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wssusername-token-profile-
            1.0#PasswordDigest">tuOSpGlFlIXsozq4HFNeeGeFLEI=</wsse:Password>
            <wsse:Nonce>LKqI6G/AikKCQrN0zqZFlg==</wsse:Nonce>
            <wsu:Created>2010-09-16T07:50:45Z</wsu:Created>
            </wsse:UsernameToken>
          </wsse:Security>
        </SOAP-ENV:Header>
      <SOAP-ENV:Body>
        <tds:SetHostname>
          <tds:Name>camera1</tds:Name>
       </tds:SetHostname>
     </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

我希望访问此XML请求中发送的username

在Soap生成的SoapServer.c中,它有一个名为

的函数
     `SOAP_FMAC5 int SOAP_FMAC6 soap_serve___tds__SetHostname(struct soap *soap)

以下函数是将xml解析为SOAP结构的地方,

`

SOAP_FMAC5 int SOAP_FMAC6 soap_serve___tds__SetHostname(struct soap *soap) {

struct __tds__SetHostname soap_tmp___tds__SetHostname;
struct _tds__SetHostnameResponse tds__SetHostnameResponse;
soap_default__tds__SetHostnameResponse(soap, &tds__SetHostnameResponse);
soap_default___tds__SetHostname(soap, &soap_tmp___tds__SetHostname);
soap->encodingStyle = NULL;
if (!soap_get___tds__SetHostname(soap, &soap_tmp___tds__SetHostname, "-tds:SetHostname", NULL))
    return soap->error;
if (soap_body_end_in(soap)
 || soap_envelope_end_in(soap)
 || soap_end_recv(soap))
    return soap->error;
soap->error = __tds__SetHostname(soap, soap_tmp___tds__SetHostname.tds__SetHostname, &tds__SetHostnameResponse);
if (soap->error)
    return soap->error;
soap_serializeheader(soap);
soap_serialize__tds__SetHostnameResponse(soap, &tds__SetHostnameResponse);
if (soap_begin_count(soap))
    return soap->error;
if (soap->mode & SOAP_IO_LENGTH)
{   if (soap_envelope_begin_out(soap)
     || soap_putheader(soap)
     || soap_body_begin_out(soap)
     || soap_put__tds__SetHostnameResponse(soap, &tds__SetHostnameResponse, "tds:SetHostnameResponse", NULL)
     || soap_body_end_out(soap)
     || soap_envelope_end_out(soap))
         return soap->error;
};
if (soap_end_count(soap)
 || soap_response(soap, SOAP_OK)
 || soap_envelope_begin_out(soap)
 || soap_putheader(soap)
 || soap_body_begin_out(soap)
 || soap_put__tds__SetHostnameResponse(soap, &tds__SetHostnameResponse, "tds:SetHostnameResponse", NULL)
 || soap_body_end_out(soap)
 || soap_envelope_end_out(soap)
 || soap_end_send(soap))
return soap->error;
return soap_closesock(soap);
  }

` 所以试图在这种方法中访问/打印用户名(从现在的基本访问开始)

fprintf(stderr,"%s\n",soap->header->wsse__Security->UsernameToken->Username);

但是每次打印一些垃圾值,通过指向结构来访问Username也是完全正确的,如果有人给出了关于如何访问SOAP头的一些指示,那将会有用。

1 个答案:

答案 0 :(得分:0)

这取决于您在哪里打印此用户名,因为它会删除最后的结构。 如果您只想将用户名从客户端发送到服务器,则将其作为参数发送到soap_send / soap_call(根据sync / async函数)函数。检查soap客户端服务器通信以获取更多详一方面,你只是发送信息和其他结束接收肥皂将在内部照顾的其他事情。