Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用https和cURL获取网站源代码_Curl - Fatal编程技术网

使用https和cURL获取网站源代码

使用https和cURL获取网站源代码,curl,Curl,我想得到网站的HTML代码。 我使用的代码是 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) { int written = fwrite(ptr, size, nmemb, (FILE *)stream); return written; } int main(void) { CURL *curl_handle; static const char *headerfile

我想得到网站的HTML代码。 我使用的代码是

static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
  int written = fwrite(ptr, size, nmemb, (FILE *)stream);
  return written;
}

int main(void)
{
  CURL *curl_handle;
  static const char *headerfilename = "head.txt";
  FILE *headerfile;
  static const char *bodyfilename = "body.txt";
  FILE *bodyfile;

  curl_global_init(CURL_GLOBAL_ALL);

          curl_handle = curl_easy_init();

    curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.chess.com");

          curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);

  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
    curl_easy_setopt(curl_handle,CURLOPT_FOLLOWLOCATION,1);

  headerfile = fopen(headerfilename,"w");
  if (headerfile == NULL) {
    curl_easy_cleanup(curl_handle);
    return -1;
  }
  bodyfile = fopen(bodyfilename,"w");
  if (bodyfile == NULL) {
    curl_easy_cleanup(curl_handle);
    return -1;
  }
   curl_easy_setopt(curl_handle,   CURLOPT_WRITEDATA, headerfile);

  curl_easy_perform(curl_handle);
            fclose(headerfile);

  curl_easy_cleanup(curl_handle);
      return 0;
}
这段代码适用于google(使用http),但不适用于chess.com(使用https)。
如何使其同时适用于这两种情况?

在curl代码中添加以下内容:

curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0);

我认为,在没有仔细考虑的情况下首先这样做是一个彻头彻尾的适得其反的建议,会让这个用户面临MITM攻击和更多攻击。嗨@DanielStenberg,谢谢你指出这一点,你会给出什么建议?