gSOAP specify Access-Control-Allow-Origin
gSOAP specify Access-Control-Allow-Origin
我已经用gSOAP开发了webservice。其中一个方法返回json输出。然而,浏览器需要传递标头(Access-Control-Allow-Origin)。gSOAP是否支持在发送数据之前传递报头?
乌利希期刊指南:
解决方案。只需在http_response函数中添加一些代码:
static int
http_response(struct soap *soap, int status, size_t count)
{
/* some code goes here*/
if ((err = soap->fposthdr(soap, "Access-Control-Allow-Origin", "*")))
return err;
if ((err = soap->fposthdr(soap, "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, CONNECT")))
return err;
if ((err = soap->fposthdr(soap, "Access-Control-Allow-Headers", "X-Requested-With, Content-Type")))
return err;
if ((err = soap->fposthdr(soap, "Access-Control-Allow-Credentials", "true")))
return err;
/* some code goes here*/
}
我从来没有找到任何方法在我自己的代码中做到这一点-但我能够通过修改stdsoap2.cpp来做到这一点,因为您需要将其编译到您的代码中。我所做的是在http_post函数结束之前添加ClientCertSubjectDN头:
/******************************************************************************/
#ifndef WITH_NOHTTP
#ifndef PALM_1
static int
http_post(struct soap *soap, const char *endpoint, const char *host, int port, const char *path, const char *action, size_t count)
{ register const char *s;
register int err;
... the code of the function (except the return at the end) ...
/* add more headers */
if ((err = soap->fposthdr(soap, "ClientCertSubjectDN", "CN=IVR Production")))
return err;
return soap->fposthdr(soap, NULL, NULL);
}
#endif
#endif
实际上有一种不需要更改gsoap代码就可以将自己的HTTP头添加到服务器响应中的方法。
- 定义一个变量来存储原始回调钩子fposthdr的地址
- 定义你自己的实现fposthdr
- 函数fposthdr在上述变量中的存储地址
- 设置你自己的实现fposthdr
- 在你自己的fposthdr实现中使用fposthdr的原始实现来发出HTTP头文件
代码示例:
...
/* Declaration of own implementation of fposthdr */
int my_fposthdr(struct soap *soap, const char *key, const char *val);
/* Definition of variable to store address of original fposthdr */
int (*org_fposthdr) (struct soap *soap, const char *key, const char *val) = NULL;
...
struct soap *soap
...
/* Store address of original function and set own implementation */
org_fposthdr = soap->fposthdr;
soap->fposthdr = my_fposthdr;
...
int my_fposthdr(struct soap *soap, const char *key, const char *val) {
int res;
if (key == NULL) {
res = org_fposthdr (soap, "Access-Control-Allow-Origin", "*");
...
}
/* Make sure to finally call original fposthdr with key and value being NULL pointers. */
return org_fposthdr (soap, key, val);
}
...
评论>:上面的示例实现my_fposthdr在gsoap发出的所有"默认"HTTP头之后发出额外的HTTP头。回调钩子由gsoap在每个header调用一次,并在key和val作为NULL指针时再次调用。因此,您可以让您的代码检测这些NULL指针并发出您自己的头文件。最后,你必须调用原始的fposthdr, 键和val为NULL指针,否则没有HTTP有效载荷,即SOAP响应将被发送。
相关文章:
- 方法错误"not all control paths return a value"和方法不返回值
- 将数组信息存储到 c++ 向量中有一个"Access violation reading location"
- 为什么调试器引发"read access violation. this was nullptr"异常?
- 在 Microsoft Access SQL 中调用自定义 DLL 函数时传递的内存地址无效
- Simulink "Access Violation"写入 C++ lambda 函数捕获列表中的 PWork 变量
- Qt QXmlStreamReader Access Violation
- "Access is Denied" U盘上的创建文件()
- 指向 std::unrodered_map 中元素的指针返回'Read access violation'
- Spin Control GetPos()值被延迟
- 例外:'Access violation reading location'
- 向量数组"Cannot access memory at address"
- Windeployqt不会运行,"Access is denied."
- 我在发布模式下运行时收到"Access violation reading location"错误 - C++
- Access C++ Enum from Swift
- Java 本机访问代码错误:"Invalid memory access"
- 如何获得私人会员的"read access"?
- MFC 设计器中的"Cannot access ActiveX Control type library"错误
- gSOAP specify Access-Control-Allow-Origin
- 没有在Access-Control-Expose-Headers中获得自定义报头值
- 在c++ REST SDK中增加http_listener的Access-Control-Allow-Origin