使用libcurl发送SOAP请求

时间:2011-09-11 01:45:50

标签: c soap libcurl

我可以理解使用libcurl发送REST请求的简单sendrecv示例。现在我想使用libcurl发送SOAP请求。我已经更改了REST请求代码以实现相同,但代码不起作用。我的代码是:

#include <stdio.h>
#include <string.h>
#include <curl/curl.h>

/* Auxiliary function that waits on the socket. */ 
static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
{
  struct timeval tv;
  fd_set infd, outfd, errfd;
  int res;

  tv.tv_sec = timeout_ms / 1000;
  tv.tv_usec= (timeout_ms % 1000) * 1000;

  FD_ZERO(&infd);
  FD_ZERO(&outfd);
  FD_ZERO(&errfd);

  FD_SET(sockfd, &errfd); /* always check for error */ 

  if(for_recv)
  {
    FD_SET(sockfd, &infd);
  }
  else
  {
    FD_SET(sockfd, &outfd);
  }

  /* select() returns the number of signalled sockets or -1 */ 
  res = select(sockfd + 1, &infd, &outfd, &errfd, &tv);
  return res;
}

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>";
  curl_socket_t sockfd; /* socket */ 
  long sockextr;
  size_t iolen;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://thermo.sdsu.edu/servlet/ThermodynamicProperties/ThermodynamicPropertiesService");
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request);
    /* 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;
    }

    /* Extract the socket from the curl handle - we'll need it for waiting.
     * Note that this API takes a pointer to a 'long' while we use
     * curl_socket_t for sockets otherwise.
     */ 
    res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr);

    if(CURLE_OK != res)
    {
      printf("Error: %s\n", curl_easy_strerror(res));
      return 1;
    }

    sockfd = sockextr;

    /* wait for the socket to become ready for sending */ 
    if(!wait_on_socket(sockfd, 0, 60000L))
    {
      printf("Error: timeout.\n");
      return 1;
    }

    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;
    }

我觉得我在构建SOAP请求时做得不对。任何帮助都会很棒,因为这对我来说是一个全新的领域。

1 个答案:

答案 0 :(得分:1)

SOAP消息比REST消息复杂得多。

我的建议是下载一个更好的SOAP实用程序,如“SOAPUI”,并使用它来构建和测试格式良好的SOAP请求。一旦有了工作请求,您就可以将其粘贴到C程序中。

如果你做的事情比发送一个简单的预格式化请求更复杂的话,我建议你使用像libxml2这样的库(如果你不介意使用C ++的Xerces)来构建你的SOAP消息。

SOAPUI是一个基于Java的测试实用程序SOAP服务。它可以从给定的WSDL生成消息,生成响应,浏览消息等。 更多信息:SOAPUI

相关问题