https client in C

* https_client.c

cpp 复制代码
/* 
 * You'll need to have OpenSSL installed on your system for this to work.
 * cc -g https_client.c -lssl -lcrypto
 * ./a.out
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <arpa/inet.h>
#include <netdb.h>

#define HOST "www.douyin.com"
#define PORT 443  // HTTPS port

// Function to create and connect a socket
int create_socket(const char *host, const char *port) {
    struct addrinfo hints, *res;
    int sockfd;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;

    if (getaddrinfo(host, port, &hints, &res) != 0) {
        perror("getaddrinfo");
        exit(EXIT_FAILURE);
    }

    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (sockfd == -1) {
        perror("socket");
        exit(EXIT_FAILURE);
    }

    if (connect(sockfd, res->ai_addr, res->ai_addrlen) == -1) {
        perror("connect");
        close(sockfd);
        exit(EXIT_FAILURE);
    }

    freeaddrinfo(res);
    return sockfd;
}

// Function to initialize OpenSSL and create an SSL context
SSL_CTX* initialize_ssl() {
    SSL_CTX *ctx;
    SSL_library_init();
    SSL_load_error_strings();
    OpenSSL_add_all_algorithms();

    ctx = SSL_CTX_new(TLS_client_method());
    if (ctx == NULL) {
        ERR_print_errors_fp(stderr);
        exit(EXIT_FAILURE);
    }

    return ctx;
}

// Function to send an HTTP GET request and receive the response
void send_https_request(SSL *ssl) {
    // HTTP GET request
    const char *request = "GET / HTTP/1.1\r\n"
                          "Host: " HOST "\r\n"
                          "Connection: close\r\n"
                          "\r\n";

    // Send the HTTP request over SSL
    SSL_write(ssl, request, strlen(request));

    // Receive and print the HTTP response
    char buffer[4096];
    int bytes;
    while ((bytes = SSL_read(ssl, buffer, sizeof(buffer) - 1)) > 0) {
        buffer[bytes] = '\0';
        printf("%s", buffer);
    }
}

int main() {
    const char *hostname = HOST;
    const char *port = "443";

    // Initialize SSL context
    SSL_CTX *ctx = initialize_ssl();

    // Create and connect the socket
    int sockfd = create_socket(hostname, port);

    // Create an SSL object
    SSL *ssl = SSL_new(ctx);
    SSL_set_fd(ssl, sockfd);

    // Establish the SSL connection
    if (SSL_connect(ssl) != 1) {
        ERR_print_errors_fp(stderr);
        exit(EXIT_FAILURE);
    }

    printf("SSL handshake completed successfully.\n");

    // Send the HTTP request and print the response
    send_https_request(ssl);

    // Clean up
    SSL_shutdown(ssl);
    SSL_free(ssl);
    close(sockfd);
    SSL_CTX_free(ctx);
    EVP_cleanup();

    return 0;
}

$ cc -g https_client.c -lssl -lcrypto

$ ./a.out

SSL handshake completed successfully.

HTTP/1.1 403 Forbidden

Server: Tengine

Content-Length: 0

Connection: close

Date: Fri, 28 Feb 2025 01:56:51 GMT

X-TT-System-Error: 3

Proxy-Status: 0000201403062000

Strict-Transport-Security: max-age=31536000; includeSubDomains; preload

Access-Control-Allow-Credentials: true

x-tt-trace-host: 0158941ad0ccfeec46825ab9fc54a041b7522659c6025178d87b37b30a129970ae1c79d103c0869a874573eb2280b0f49168edd6e5c61d3174905406bf3cafc890

x-tt-trace-tag: id=03;cdn-cache=miss;type=dyn

x-tt-trace-id: 00-250228095651DE5EF1BEE800484DA0B3-4CE170E36114905B-00

X-TT-LOGID: 20250228095651DE5EF1BEE800484DA0B3

x-alicdn-da-ups-status: endOs,0,403

Via: cache29.l2nu16[16,0], ens-cache4.es6[172,0]

server-timing: cdn-cache;desc=MISS,edge;dur=156,origin;dur=16

Timing-Allow-Origin: *

EagleId: 2ff62d9817407078115605848e

相关推荐
itman30111 分钟前
C语言入门:掌握编程底层逻辑与核心技能
c语言·编程入门·系统开发·底层逻辑·核心技能
网域小星球1 小时前
C语言从0入门(九)|函数进阶:嵌套调用、递归与变量作用域精讲
c语言·vs2022·嵌套调用·变量作用域·递归函数
m0_716765231 小时前
数据结构--顺序表的插入、删除、查找详解
c语言·开发语言·数据结构·c++·学习·算法·visual studio
念恒123061 小时前
Linux权限
linux·c语言
蚊子码农1 小时前
每日一题--关于转向的思考
c语言
c++圈来了个新人1 小时前
C++类和对象(上)
c语言·开发语言·数据结构·c++·考研
晏宁科技YaningAI1 小时前
分布式通信系统的容错机制
网络协议·微服务·系统架构·gateway·信息与通信·paas
钢琴上的汽车软件9 小时前
C 语言中const与指针:三种常见写法辨析
c语言·指针和const
ZK_H10 小时前
嵌入式c语言——关键字其6
c语言·开发语言·计算机网络·面试·职场和发展
ambition2024210 小时前
从暴力搜索到理论最优:一道任务调度问题的完整算法演进历程
c语言·数据结构·c++·算法·贪心算法·深度优先