如何从客户端通过gsoap C ++的标头中传递非xml数据?

时间:2018-09-12 09:08:30

标签: c++ header gsoap

我正在使用gsoap c ++库进行Java Web服务调用。

我可以通过调用json_call传递json文件,但是我想添加标题信息。为此,我将结构SOAP_ENV__Header修改为:

struct ns3__Header   {       
  char *username;          
  char* password;   };   
struct SOAP_ENV__Header    {
  #ifdef WITH_NOEMPTYSTRUCT
  char dummy;    
  #endif
  struct ns3__Header *ns3__MyHeader;
};

现在我可以在标头中添加值,但是我的问题是它们正以xml格式添加到标头中。但是我只想要tag:value格式。如何实现?

当我传递标题时,它看起来像:

<?xml version="1.0" encoding="UTF-8"?>   <username id="_300">        <ns3:MyHeader> 
        in soap_out_SOAP_ENV__Header
    </ns3:MyHeader>   </username>   {     "add":      {       "i": 10,      "j": 20,  
 }   }

但是它看起来应该像:

username:xyz
password:abcd
...
...
{
  "add": 
  {
    "i": 10,
    "j": 20,

  }
} 

2 个答案:

答案 0 :(得分:0)

Soap_Env_header通过xml发送默认值,无法将其推送到json中,请尝试其他库,希望是cpprest(Casablanca)

答案 1 :(得分:0)

最后,我找到了一种在c ++ gsoap的rest调用中传递标头信息的解决方案-

#include "json.h"
#include <string.h>
#include "jsonStub.h"

struct Namespace namespaces[] = { {NULL, NULL} };
int main()
{
   struct soap *ctx = soap_new1(SOAP_XML_NOTYPE);
   soap_init(ctx);
   struct value *request = new_value(ctx);
   struct value response;

   ctx->sendfd = 1;   
   ctx->http_extra_header = "userName:abcd\r\npassword:xyz";
   *string_of(value_at(value_at(request, "add"), "i")) = "10";
   *string_of(value_at(value_at(request, "add"), "j")) = "20";

   json_write(ctx, request);
   printf("\n");
   if (json_call(ctx, "endpoint",request, &response))
   {
         printf( "json call failed " );
         soap_print_fault(ctx, stderr);
         printf("\n%d", ctx->error);
         printf("\n1: SOAP faultcode = %s\n", *soap_faultcode(ctx));
         printf("2: SOAP faultstring = %s\n", *soap_faultstring(ctx));
         printf("3: SOAP faultdetail = %s\n\n", *soap_faultdetail(ctx));
         soap_print_fault_location(ctx, stderr);

   }

   else
   {
         printf("Success !!!");
         json_write(ctx, &response);
   }

   soap_destroy(ctx);
   soap_end(ctx);
   soap_free(ctx);
   return 0;

}