📜  你能验证 adsense 没有 ssl 证书吗 - C++ (1)

📅  最后修改于: 2023-12-03 15:36:25.525000             🧑  作者: Mango

你能验证 Adsense 没有 SSL 证书吗 - C++

在验证 Adsense 是否有 SSL 证书时,可以使用 C++ 语言实现以下方法:

  1. 使用 cURL 库发送 HTTPS 请求来检测网站是否有 SSL 证书。
#include <iostream>
#include <curl/curl.h>

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

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "https://google.com/");
        curl_easy_setopt(curl, CURLOPT_NOBODY, true);
        res = curl_easy_perform(curl);
        if(res == CURLE_OK) {
            long response_code;
            curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
            if (response_code == 200) {
                std::cout << "The website has SSL certificate!" << std::endl;
            } else {
                std::cout << "The website does not have SSL certificate!" << std::endl;
            }
        } else {
            std::cout << "Failed to check SSL certificate." << std::endl;
        }
        curl_easy_cleanup(curl);
    }

    return 0;
}
  1. 获取网站时的 HTTP 头部信息来检查是否有相关证书信息。
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>

std::string get_http_header(const std::string &hostname, int port, const std::string &path) {
    struct sockaddr_in server_addr;

    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr(hostname.c_str());
    server_addr.sin_port = htons(port);

    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {
        return "";
    }

    if (connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
        return "";
    }

    std::stringstream ss;
    ss << "GET " << path << " HTTP/1.1\r\n"
       << "Host: " << hostname << "\r\n"
       << "Connection: close\r\n"
       << "User-Agent: Mozilla/5.0\r\n"
       << "Accept-Language: en-US,en;q=0.5\r\n\r\n";

    std::string request = ss.str();
    int ret = send(sockfd, request.c_str(), request.length(), 0);
    if (ret < 0) {
        return "";
    }

    char buffer[1024];
    std::string response;
    while (true) {
        memset(buffer, 0, sizeof(buffer));
        ret = recv(sockfd, buffer, sizeof(buffer)-1, 0);
        if (ret <= 0) {
            break;
        }
        response.append(buffer, ret);
    }

    return response;
}

int main() {
    std::string header = get_http_header("google.com", 80, "/");
    if (header.find("ssl_certificate") != std::string::npos) {
        std::cout << "The website has SSL certificate!" << std::endl;
    } else {
        std::cout << "The website does not have SSL certificate!" << std::endl;
    }

    return 0;
}

以上是两种用 C++ 验证 Adsense 是否有 SSL 证书的方法,可以根据自己的需要选择其中一种进行使用。