rorono
7/27/2016 - 12:14 AM

curllib test

curllib test

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

void check_verification() {
#ifdef SKIP_PEER_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
   * A LOT LESS SECURE.
   *
   * 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);
#endif

#ifdef SKIP_HOSTNAME_VERIFICATION
  /*
   * 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);
#endif
}

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

  char *location;
  long response_code;

  curl_global_init(CURL_GLOBAL_DEFAULT);

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "https://acpt.jsysneo.fukuoka-u.ac.jp/ActiveCampus/index.html");
    // curl_easy_setopt(curl, CURLOPT_URL, "https://google.com");
    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 */

    check_verification();


  /* 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",
              curl_easy_strerror(res));
      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");
    }else{
      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 */
    curl_easy_cleanup(curl);
  }else{
    fprintf(stderr, "Curl init failed!\n");
    return 1;
  }

  curl_global_cleanup();

  return 0;
}