HTML的页面的写法
根据用户需求是想要使用POST还是GET,则在form表单的mothd赋值为post或者get
post:<form action="calc" method="post" name="Calc">
get:<form action="calc" method="get" name="Calc">
http_post,http_get插件函数
gsoap自带post和get的插件,路径:gsoap-2.8/gsoap/plugin/httppost.c httpget.c
在两个文件中定义了两个重要的函数,http_post,http_get这两个函数用于被作为插件调用,
在gsoap的主函数中用如下方法调用:
/* Register HTTP GET plugin */
if (soap_register_plugin_arg(&soap, http_get, (void*)http_get_handler))
soap_print_fault(&soap, stderr);
/* Register HTTP POST plugin */
if (soap_register_plugin_arg(&soap, http_form, (void*)http_form_handler))
soap_print_fault(&soap, stderr);
插件的作用就是当用户的请求是POST时就会自动调用http_form_handler,否则调用http_get_handler
http_get_handler和http_form_handler请求解析函数
其中(void*)http_get_handler和(void*)http_form_handler是需要自定义的函数
int http_get_handler(struct soap*); /* HTTP get handler */
int http_form_handler(struct soap*); /* HTTP form handler */
在这两个函数中需要解析用户的请求,得到用户请求的路径是什么,其中soap->path就是路径,如下代码
if (!strcmp(soap->path, "/calc"))
return calcget(soap );
当路径是”/calc“就调用calcget函数处理请求。
请求处理函数
如上面的函数calcget(soap)
如果函数是被http_form_handler调用则需要在函数中使用form(soap)函数解析用户请求中的参数
如果函数是被http_get_handler调用则需要在函数中使用query(soap)函数解析用户请求中的参数
实例:
int calcpost(struct soap *soap)
{ int o = 0, a = 0, b = 0, val;
char buf[256];
char *s = form(soap); /* get form data from body */
while (s)
{ char *key = query_key(soap, &s); /* decode next key */
char *val = query_val(soap, &s); /* decode next value (if any) */
if (key && val)
{ if (!strcmp(key, "o"))
o = val[0];
else if (!strcmp(key, "a"))
a = strtol(val, NULL, 10);
else if (!strcmp(key, "b"))
b = strtol(val, NULL, 10);
}
}
switch (o)
{ case 'a':
val = a + b;
break;
case 's':
val = a - b;
break;
case 'm':
val = a * b;
break;
case 'd':
val = a / b;
break;
default:
return soap_sender_fault(soap, "Unknown operation", NULL);
}
soap_response(soap, SOAP_HTML);
sprintf(buf, "<html>value=%d</html>", val);
soap_send(soap, buf);
soap_end_send(soap);
return SOAP_OK;
}