13397158231   jevian_ma@worldflying.cn

学习篇 --- 利用C语言写http以及https客户端 - 1.1版本

2020-03-11 10:47:23

本文附上http与https客户端C语言源代码以及编译命令,加密使用的是openssl,使用的是tls1.2版本加密。

首先,安装编译环境apt-get install gcc libssl-dev

https版本客户端编译命令为gcc -o client client.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>

#define host   "www.baidu.com"
#define port    80

char sendbuff[1024*50];
char recvbuff[1024*50];

int main () {
    int fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(port);
    /* 如果host是IP,直接使用下面方式
    sin.sin_addr.s_addr = inet_addr(host);
    */
    struct hostent *ip = gethostbyname(host); // 域名dns解析
    if(ip == NULL) {
        printf("gethostbyname error");
        return 0;
    }
    sin.sin_addr = *(struct in_addr*)ip->h_addr_list[0];
    if(connect(fd,(struct sockaddr*)&sin, sizeof(sin)) < 0) {
        printf("connect error");
        return 0;
    }
    strcpy(sendbuff,"GET /index.html HTTP/1.1\r\n"
                    "Host: "host"\r\n"
                    "User-Agent: www.worldflying.cn client\r\n"
                    "Accept: */*\r\n"
                    "Cache-Control: no-cache\r\n"
                    "Accept-Encoding: gzip, deflate, br\r\n"
                    "Connection: keep-alive\r\n\r\n");
    write(fd, sendbuff, strlen(sendbuff));
    while (1) {
        memset(recvbuff, 0, sizeof(recvbuff));
        if (read(fd,recvbuff,sizeof(recvbuff)-1) <= 0) {
            break;
        }
        printf("%s\n", recvbuff);
    }
    close(fd);
}

https版本客户端编译命令为gcc -o client client.c -lcrypto -lssl

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <openssl/ssl.h>

#define host   "www.baidu.com"
#define port    443

char sendbuff[1024*50];
char recvbuff[1024*50];

int main() {
    SSL_library_init();
    OpenSSL_add_all_algorithms();
    SSL_load_error_strings();
    SSL_CTX *ctx = SSL_CTX_new(TLS_client_method());
    if(ctx == NULL) {
        return 0;
    }
    int fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(port);
    /* 如果host是IP,直接使用下面方式
    sin.sin_addr.s_addr = inet_addr(host);
    */
    struct hostent *ip = gethostbyname(host); // 域名dns解析
    if(ip == NULL) {
        printf("gethostbyname error");
        return 0;
    }
    sin.sin_addr = *(struct in_addr*)ip->h_addr_list[0];
    if(connect(fd,(struct sockaddr*)&sin, sizeof(sin)) < 0) {
        printf("connect error");
        return 0;
    }
    SSL *ssl = SSL_new(ctx);
    SSL_set_fd(ssl, fd);
    if(SSL_connect(ssl) == -1) {
        printf("ssl connect error");
        return 0;
    }

    strcpy(sendbuff,"GET /index.html HTTP/1.1\r\n"
                    "Host: "host"\r\n"
                    "User-Agent: www.worldflying.cn client\r\n"
                    "Accept: */*\r\n"
                    "Cache-Control: no-cache\r\n"
                    "Accept-Encoding: gzip, deflate, br\r\n"
                    "Connection: keep-alive\r\n\r\n");
    SSL_write(ssl,sendbuff, strlen(sendbuff));

    while (1) {
        memset(recvbuff, 0, sizeof(recvbuff));
        if (SSL_read(ssl, recvbuff, sizeof(recvbuff)-1) <= 0) {
            break;
        }
        printf("%s\n", recvbuff);
    }
    SSL_shutdown(ssl);
    SSL_free(ssl);
    close(fd);
    SSL_CTX_free(ctx);
}


文章来源:沃航科技

优秀产品推荐:可编程网络IO控制器

上一篇:使用rufus制作U盘启动盘遇到Unable to send request: This syste

下一篇:wfproxy是沃航科技推出的最新工具,旨在解决企业内网穿透,远程办公等使用场景

联系我们

  • 地址:武汉市东湖高新开发区光谷总部国际1栋2412室
  • QQ:932773931
  • 电话:027-59761089-806
  • 手机:13397158231
  • 邮箱:jevian_ma@worldflying.cn

关注公众号

扫码添加微信

沃航(武汉)科技有限公司版权所有

备案号:鄂ICP备16014230号-1

本网站由提供CDN加速/云存储服务