7/27/2016 - 12:14 AM

curllib test

curllib test

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

void check_verification() {
   * If you want to connect to a site who isn't using a certificate that is
   * signed by one of the certs in the CA bundle you have, you can skip the
   * verification of the server's certificate. This makes the connection
   * If you have a CA cert for the server stored someplace else than in the
   * default bundle, then the CURLOPT_CAPATH option might come handy for
   * you.
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);

   * If the site you're connecting to uses a different host name that what
   * they have mentioned in their server certificate's commonName (or
   * subjectAltName) fields, libcurl will refuse to connect. You can skip
   * this check, but this will make the connection less secure.
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

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

  char *location;
  long response_code;


  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "");
    // curl_easy_setopt(curl, CURLOPT_URL, "");
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36");
    // curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookie.txt"); /* start cookie engine(read only) */
    curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie.txt");  /* start cookie engine */


  /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK){
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
      return 1;

    res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
    if((res == CURLE_OK) && ((response_code / 100) != 3)) {
      /* a redirect implies a 3xx response code */
      fprintf(stderr, "Not a redirect.\n");
      res = curl_easy_getinfo(curl, CURLINFO_REDIRECT_URL, &location);
       if((res == CURLE_OK) && location) {
        /* This is the new absolute URL that you could redirect to, even if
         * the Location: response header may have been a relative URL. */
        printf("Redirected to: %s\n", location);

    /* always cleanup */
    fprintf(stderr, "Curl init failed!\n");
    return 1;


  return 0;