关于 SOAP 响应字符串的句柄
我正在尝试向 Web 服务发送 SOAP 请求并使用 C 接收返回的响应。我被定向到“simplepost.c”的示例,并使用该示例,我能够从 Web 服务发送并打印返回的 SOAP 响应在我的命令行上。但我不需要在屏幕上打印响应,而是需要响应字符串的句柄,以便能够提取 SOAP 标记内的值。我编写了以下发送和接收程序,但我无法收到正确的响应,这意味着发送或接收有问题。如果有人能帮助我找到我到底如何实现、我想要什么,那将非常有帮助。提前致谢。
我的代码:
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
/* Auxiliary function that waits on the socket. */
int main(void)
{
CURL *curl;
CURLcode res;
/* Minimalistic http request */
const char *request = "<?xml version=\"1.0\" encoding=\"utf-8\"?> <S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"xmlns:tns=\"http://ThermodynamicProperties/\"><S:Body> <tns:getSpeciesInformation> <speciesSymbol>CO2</speciesSymbol> <phase>GAS</phase> </tns:getSpeciesInformation> </S:Body> </S:Envelope>";
size_t iolen;
struct curl_slist *headerlist=NULL;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://thermo.sdsu.edu/servlet/ThermodynamicProperties/ThermodynamicPropertiesService");
/* Do not do the transfer - only connect to host */
curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
res = curl_easy_perform(curl);
if(CURLE_OK != res)
{
printf("Error: %s\n", strerror(res));
return 1;
}
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
puts("Sending request.");
/* Send the request. Real applications should check the iolen
* to see if all the request has been sent */
res = curl_easy_send(curl,request, strlen(request), &iolen);
if(CURLE_OK != res)
{
printf("Error: %s\n", curl_easy_strerror(res));
return 1;
}
puts("Reading response.");
/* read the response */
printf("ok1 \n");
char buf[10240];
res = curl_easy_recv(curl, buf, 10240, &iolen);
printf("ok2 \n");
if(CURLE_OK != res)
{
printf("Error: %s\n", strerror(res));
}
else{ printf("data %s \n", buf);
}
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
我得到的响应是: 发送请求。 阅读回应。 好的1 好的2 错误:a.out 中的 .lib 部分已损坏
I am trying to send a SOAP request to a webservice and receive the response back using C. I was directed to an example of "simplepost.c" and using that, I am able to send and print a SOAP response back from a web service on my command line. But instead of printing the response on the screen, I want a handle to the response string, so that I am able to extract the values inside the SOAP tags. I have the following send and receive program written, but i am not able to receive proper response, which means either there is problem in sending or receiving. If any one can help me locate how exactly can I achieve, what i want, it would be really helpful. Thanks in advance.
My code:
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
/* Auxiliary function that waits on the socket. */
int main(void)
{
CURL *curl;
CURLcode res;
/* Minimalistic http request */
const char *request = "<?xml version=\"1.0\" encoding=\"utf-8\"?> <S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"xmlns:tns=\"http://ThermodynamicProperties/\"><S:Body> <tns:getSpeciesInformation> <speciesSymbol>CO2</speciesSymbol> <phase>GAS</phase> </tns:getSpeciesInformation> </S:Body> </S:Envelope>";
size_t iolen;
struct curl_slist *headerlist=NULL;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://thermo.sdsu.edu/servlet/ThermodynamicProperties/ThermodynamicPropertiesService");
/* Do not do the transfer - only connect to host */
curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
res = curl_easy_perform(curl);
if(CURLE_OK != res)
{
printf("Error: %s\n", strerror(res));
return 1;
}
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
puts("Sending request.");
/* Send the request. Real applications should check the iolen
* to see if all the request has been sent */
res = curl_easy_send(curl,request, strlen(request), &iolen);
if(CURLE_OK != res)
{
printf("Error: %s\n", curl_easy_strerror(res));
return 1;
}
puts("Reading response.");
/* read the response */
printf("ok1 \n");
char buf[10240];
res = curl_easy_recv(curl, buf, 10240, &iolen);
printf("ok2 \n");
if(CURLE_OK != res)
{
printf("Error: %s\n", strerror(res));
}
else{ printf("data %s \n", buf);
}
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
The response I am getting is:
Sending request.
Reading response.
ok1
ok2
Error: .lib section in a.out corrupted
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您使用 gsoap,因为它处理所有发送/接收部分并解析和构造肥皂消息。
如果您想使用卷曲和手动解析,那么您肯定有错误的内容类型。它必须
在 SOAP 1.1 或
SOAP 1.2 中,而且我发现如果没有正确的内容类型,许多 Web 服务甚至不会响应。
但真正考虑使用工具来生成存根并只关注程序的逻辑。手动编写解析通常很糟糕。
I suggest you using gsoap, because it deals with all the send/receive part and also parses and constructs the soap messages.
If you want to work with curl and manual parsing for sure you have a wrong contanty type. It has to be
in SOAP 1.1 or
in SOAP 1.2 and I've seen that without the correct content type many web services don't even respond.
But really consider using a tool to generate the stubs and just focus on the logic of the program. Writing a parse by hand is in general bad.