c - libcurl 中的 Json 字符串

标签 c json libcurl

我可以使用 libcurl 发送 Json 字符串吗?我对两者都不熟悉。任何形式的帮助将不胜感激。基本上我想在 C 中使用 libcurl 简单发送和接收发送一个 Json 字符串

我的代码如下:

#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 = "reactantsJSON={"O=O":{"N":1}}&productsJSON=["O=O","[O]"]&temperature=2273.15&pressure=101.325";
  curl_socket_t sockfd; /* socket */ 
  long sockextr;
  size_t iolen;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://gibbs.sdsu.edu:8080/axis2/services/GibbsMinimization/solveTP");
    /* 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;
    }
    puts("Reading response.");

    /* read the response */ 
    for(;;)
    {
      char buf[1024];

      wait_on_socket(sockfd, 1, 60000L);
      res = curl_easy_recv(curl, buf, 1024, &iolen);

      if(CURLE_OK != res)
        break;

      printf("Received %u bytes.\n", iolen);
    }

    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
  return 0;
}

我得到的错误是它说一个“;”在第 40 行中丢失,它只是 json 字符串,即使我有一个“;”在那条线上。看起来我需要发送 json 字符串来做一些我不知道的事情。我是否需要在编译代码时指定一些标志。

最佳答案

const char *request = "reactantsJSON={"O=O":{"N":1}}&productsJSON=["O=O","[O]"]&temperature=2273.15&pressure=101.325";

您需要在字符串文字中转义 "

const char *request = "reactantsJSON={\"O=O\":{\"N\":1}}&productsJSON=[\"O=O\",\"[O]\"]&temperature=2273.15&pressure=101.325";

关于c - libcurl 中的 Json 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7351406/

相关文章:

搜索数组以查找字符序列的 C 程序

c - 如何在 C 程序中将 stdin 重定向回(在 Windows 下)

java - Gson在json中存储对象类型

C++ 静态链接 curl

c - 在 C 中播放 mp3 文件

http - 是否可以使 libcurl 压缩 http 消息的数据?

c - C中的整数数组初始化错误

c - 如何将多个值作为参数传递给 C 中的线程?

java - java代码中解析json对象

javascript - Ember 模型与使用 REST 作为 JSON 接收的数据 Hook