开发者

Handling responses in libcurl

i have a code to send a Form Post with login credentials to a webpage. it looks like this

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 receive开发者_运维知识库s 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);
  }

now i need to know is how do i handle the responses i get back from the server? i need to store the response into a string and then work on it.


This question shows you how to get the response data string

In short, you need to use curl_easy_setopt to set the option CURLOPT_WRITEFUNCTION to a callback function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜