3

我有一个代码可以将带有登录凭据的表单帖子发送到网页。看起来像这样

CURL *curl;
  CURLcode res;

  struct curl_httppost *formpost=NULL;
  struct curl_httppost *lastptr=NULL;
  struct curl_slist *headerlist=NULL;
  static const char buf[] = "Expect:";

  curl_global_init(CURL_GLOBAL_ALL);

  /* Fill in the username */
  curl_formadd(&formpost,
             &lastptr,
             CURLFORM_COPYNAME, "user",
             CURLFORM_COPYCONTENTS, "username",
             CURLFORM_END);

  /* Fill in the password */
  curl_formadd(&formpost,
             &lastptr,
             CURLFORM_COPYNAME, "pass",
             CURLFORM_COPYCONTENTS, "password",
             CURLFORM_END);


  /* Fill in the submit field too, even if this is rarely needed */ 
  curl_formadd(&formpost,
               &lastptr,
               CURLFORM_COPYNAME, "submit",
               CURLFORM_COPYCONTENTS, "send",
               CURLFORM_END);

  curl = curl_easy_init();
  /* initalize custom header list (stating that Expect: 100-continue is not
     wanted */ 
  headerlist = curl_slist_append(headerlist, buf);
  if(curl) {
    /* what URL that receives this POST */ 
    curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:8000/index.html");

      /* only disable 100-continue header if explicitly requested */ 
      curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
    res = curl_easy_perform(curl);

    /* always cleanup */ 
    curl_easy_cleanup(curl);

    /* then cleanup the formpost chain */ 
    curl_formfree(formpost);
    /* free slist */ 
    curl_slist_free_all (headerlist);
  }

现在我需要知道如何处理从服务器返回的响应?我需要将响应存储到一个字符串中,然后处理它。

4

1 回答 1

3

这个问题向您展示了如何获取响应数据字符串

简而言之,您需要使用curl_easy_setopt将选项设置CURLOPT_WRITEFUNCTION为回调函数。

于 2010-04-22T09:21:04.717 回答