admin管理员组

文章数量:1630183

#ifndef LIBCURL_COOKIE_INTERFACE_HPP
#define LIBCURL_COOKIE_INTERFACE_HPP
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <curl/curl.h>

static void print_cookie(CURL* pCurl){
    CURLcode res;
    struct curl_slist *pCookies;
    struct curl_slist *pNc;
    int i;

    printf("Cookies, curl knows:\n");
    res = curl_easy_getinfo(pCurl,CURLINFO_COOKIELIST,&pCookies);
    if(CURLE_OK !=res){
        fprintf(stderr,"Curl curl_easy_getinfo failed: %s\n",curl_easy_strerror(res));
        exit(1);
    }
    pNc = pCookies;
    i = 1;
    while (pNc) {
        printf("[%d]:%s\n",i,pNc->data);
        pNc = pNc->next;
        i++;
    }
    if(1 == i){
        printf("(none)\n");
    }
    curl_slist_free_all(pCookies);
}

int test_cookie_interface(){
    CURL *pCurl;
    CURLcode res;
    curl_global_init(CURL_GLOBAL_ALL);
    pCurl = curl_easy_init();
    if(pCurl){
        char nline[256];

        curl_easy_setopt(pCurl,CURLOPT_URL,"https://www.example/");
        curl_easy_setopt(pCurl,CURLOPT_VERBOSE,0L);
        curl_easy_setopt(pCurl,CURLOPT_COOKIEFILE,""); //start cookies engin
        curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 0); //非常重要的两行,源代码实例中是没有的
        curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 0); //非常重要的两行,源代码实例中是没有的
        res = curl_easy_perform(pCurl);
        if(CURLE_OK != res){
            fprintf(stderr,"Curl performed failed: %s\n",curl_easy_strerror(res));
            return 1;
        }
        print_cookie(pCurl);

        printf("Erasing curl's knowledge of cookies !\n");
        curl_easy_setopt(pCurl,CURLOPT_COOKIELIST,"All");
        print_cookie(pCurl);
        printf("-------------------------------------------------------------------------\n"
               "Setting a cookie  \"PREF\" via cookied interface:\n");
        snprintf(nline ,sizeof(nline),"\%s\t%s\t%s\t%s\t%lu\t%s\t%s",".example","TRUE","/","FALSE",(unsigned long)time(NULL)+31337UL,"PREF","hello example, i like you very much !");
        res = curl_easy_setopt(pCurl,CURLOPT_COOKIELIST,nline);
        if(CURLE_OK != res){
            fprintf(stderr,"Curl curl_easy_setopt failed: %s\n",curl_easy_strerror(res));
            return 1;
        }

        snprintf(nline,sizeof(nline),"Set-Cookie: OLD_PREF=3d141414bf4209321;"
                 "expires=Sun,17-Jan=2038 19:14:07 GMT; path=/; domain=.example");
        res = curl_easy_setopt(pCurl,CURLOPT_COOKIELIST,nline);
        if(CURLE_OK != res){
            fprintf(stderr,"Curl curl_easy_setopt failed:%s\n",curl_easy_strerror(res));
            return 1;
        }

        print_cookie(pCurl);

        res = curl_easy_perform(pCurl);
        if(CURLE_OK != res){
            fprintf(stderr,"Curl perform failed: %s\n",curl_easy_strerror(res));
            return 1;
        }

        curl_easy_cleanup(pCurl);
    }else{
        fprintf(stderr,"Curl init failed!\n");
        return 1;
    }
    curl_global_cleanup();
    return 0;
}

#endif // LIBCURL_COOKIE_INTERFACE_HPP

没有如下两行代码时报错

        curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 0); //非常重要的两行,源代码实例中是没有的
        curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 0); //非常重要的两行,源代码实例中是没有的

报错信息:

*   Trying 93.184.216.34:443...
* Connected to www.example (93.184.216.34) port 443 (#0)
* ALPN, offering http/1.1
* SSL certificate problem: self signed certificate in certificate chain
* Closing connection 0
Curl performed failed: SSL peer certificate or SSH remote key was not OK
Press <RETURN> to close this window...

添加以后运行结果:

*   Trying 93.184.216.34:443...
* Connected to www.example (93.184.216.34) port 443 (#0)
* ALPN, offering http/1.1
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: C=US; ST=California; L=Los Angeles; O=Internet Corporation for Assigned Names and Numbers; OU=Technology; CN=www.example
*  start date: Nov 28 00:00:00 2018 GMT
*  expire date: Dec  2 12:00:00 2020 GMT
*  issuer: C=US; O=DigiCert Inc; CN=DigiCert SHA2 Secure Server CA
*  SSL certificate verify result: self signed certificate in certificate chain (19), continuing anyway.
> GET / HTTP/1.1
Host: www.example
Accept: */*

* old SSL session ID is stale, removing
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Age: 318847
< Cache-Control: max-age=604800
< Content-Type: text/html; charset=UTF-8
< Date: Mon, 08 Jun 2020 06:24:39 GMT
< Etag: "3147526947"
< Expires: Mon, 15 Jun 2020 06:24:39 GMT
< Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
< Server: ECS (sjc/4E76)
< Vary: Accept-Encoding
< X-Cache: HIT
< Content-Length: 1256
< 
<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
        
    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>    
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana/domains/example">More information...</a></p>
</div>
</body>
</html>
* Connection #0 to host www.example left intact
Cookies, curl knows:
(none)
Erasing curl's knowledge of cookies !
Cookies, curl knows:
(none)
-------------------------------------------------------------------------
Setting a cookie  "PREF" via cookied interface:
* Added cookie PREF="hello example, i like you very much !" for domain example, path /, expire 1591628816
* Added cookie OLD_PREF="3d141414bf4209321" for domain example, path /, expire 2147368447
Cookies, curl knows:
[1]:.example	TRUE	/	FALSE	1591628816	PREF	hello example, i like you very much !
[2]:.example	TRUE	/	FALSE	2147368447	OLD_PREF	3d141414bf4209321
* Found bundle for host www.example: 0x56343b14b0d0 [serially]
* Re-using existing connection! (#0) with host www.example
* Connected to www.example (93.184.216.34) port 443 (#0)
> GET / HTTP/1.1
Host: www.example
Accept: */*
Cookie: OLD_PREF=3d141414bf4209321; PREF=hello example, i like you very much !

* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Age: 567225
< Cache-Control: max-age=604800
< Content-Type: text/html; charset=UTF-8
< Date: Mon, 08 Jun 2020 06:24:39 GMT
< Etag: "3147526947"
< Expires: Mon, 15 Jun 2020 06:24:39 GMT
< Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
< Server: ECS (sjc/16C8)
< Vary: Accept-Encoding
< X-Cache: HIT
< Content-Length: 1256
< 
<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
        
    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>    
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana/domains/example">More information...</a></p>
</div>
</body>
</html>
* Connection #0 to host www.example left intact
Press <RETURN> to close this window...
^A

 

本文标签: libcurlcookieinterface