一个关于curl post提交返回数据的问题1

一个关于curl post提交返回数据的问题
我在curl中找到一个post提交数据的示例. 不过它在控制台所返回的数据我无法捕捉到,其实我的需求就是在程序post提交到服务器之后,将返回的数据得到。如果有用过curl的朋友请不吝赐教.
以下是示例代码,我debug追踪了半天,也没找到它返回数据放在什么地方的.

#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#define TRUE 1

char data[]="data=testdata";/* 测试数据,将testdate这个字符串作为data参数的数据*/

struct WriteThis {
  char *readptr;
  int sizeleft;
};

size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
{
  struct WriteThis *pooh = (struct WriteThis *)userp;

  if(size*nmemb < 1)
    return 0;

  if(pooh->sizeleft) {
    *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
    pooh->readptr++;                 /* advance pointer */
    pooh->sizeleft--;                /* less data left */
    return 1;                        /* we return 1 byte at a time! */
  }

  return -1;                         /* no more data left to deliver */
}

int main(void)
{
  CURL *curl;
  CURLcode res;

  struct WriteThis pooh;

  pooh.readptr = data;
  pooh.sizeleft = strlen(data);

  curl = curl_easy_init();
  if(curl) {
    /* First set the URL that is about to receive our POST. */
/**
    curl_easy_setopt(curl, CURLOPT_URL,
                     "http://receivingsite.com.pooh/index.cgi");
**/
    curl_easy_setopt(curl, CURLOPT_URL,
                     "http://www.ilcd.tv:8080/axbbs/index.jsp");
    /* Now specify we want to POST data */
    curl_easy_setopt(curl, CURLOPT_POST, TRUE);

    /* we want to use our own read function */
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);

    /* pointer to pass to our read function */
    curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);

    /* get verbose debug output please */
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);

    /*
      If you use POST to a HTTP 1.1 server, you can send data without knowing
      the size before starting the POST if you use chunked encoding. You
      enable this by adding a header like "Transfer-Encoding: chunked" with
      CURLOPT_HTTPHEADER. With HTTP 1.0 or without chunked transfer, you must
      specify the size in the request.
    */
#ifdef USE_CHUNKED
    {
      curl_slist *chunk = NULL;

      chunk = curl_slist_append(chunk, "Transfer-Encoding: chunked");
      res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
      /* use curl_slist_free_all() after the *perform() call to free this
         list again */
    }
#else
    /* Set the expected POST size. If you want to POST large amounts of data,
       consider CURLOPT_POSTFIELDSIZE_LARGE */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, pooh.sizeleft);
#endif

#ifdef DISABLE_EXPECT
    /*
      Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue"
      header.  You can disable this header with CURLOPT_HTTPHEADER as usual.
      NOTE: if you want chunked transfer too, you need to combine these two
      since you can only set one list of headers with CURLOPT_HTTPHEADER. */

    /* A less good option would be to enforce HTTP 1.0, but that might also
       have other implications. */
    {
      curl_slist *chunk = NULL;

      chunk = curl_slist_append(chunk, "Expect:");
      res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
      /* use curl_slist_free_all() after the *perform() call to free this
         list again */
    }
#endif

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);

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



这是我从网上找到稍为修改的代码。
运行:
程序 参数1 参数2
参数1为URL,参数二为要存文件名4 ?! S& l0 o. D! w
经测试运行正常
判断结果
CURL应该是通过回调函数(本程序是write_data)来把内容传输的。
希望这些信息对你有用
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>;
#include <curl/curl.h>;
#include <curl/types.h>;
#include <curl/easy.h>;
FILE *fp;  //定义FILE类型指针
size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)  //这个函数是为了符合CURLOPT_WRITEFUNCTION, 而构造的
{
    int written = fwrite(ptr, size, nmemb, (FILE *)fp);
    return written;
}
int main(int argc, char *argv[])
{
      CURL *curl;
    curl_global_init(CURL_GLOBAL_ALL); 
    curl=curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, argv[1]); 
    if((fp=fopen(argv[2],"w"))==NULL)
    {
        curl_easy_cleanup(curl);
        exit(1);
    }
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);  //CURLOPT_WRITEFUNCTION 将后继的动作交给write_data函数处理
    curl_easy_perform(curl);
    curl_easy_cleanup(curl);
  system("PAUSE");
  return 0;
}

/
很感谢hagejid,您的敬业精神值得佩服,这个问题昨天已经解决。
string Curlplus::buffer;
在curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
将数据写给buffer,然后用一个方法去返回buffer就可以了
主要代码
string Curlplus::get(const string& url) const
{
    buffer="";
    CURL *curl = curl_easy_init();
    if(curl == NULL)
    {
        cout << "curl_easy_init failed ";
    }

    _setCurlopt(curl,url);
    CURLcode code = curl_easy_perform(curl);

    if(code != CURLE_OK)
    {
        cout << "curl_easy_perform failed: "<< code;
    }
    curl_easy_cleanup(curl);
    return buffer;
}

void Curlplus::_setCurlopt(CURL *curl,const string& url) const {
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
    curl_easy_setopt(curl, CURLOPT_HEADER, 0);
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, _defaulttimeout);
    //curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Curlplus::writer);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值