基本信息
源码名称:linux c++实现https
源码大小:4.08KB
文件格式:.cpp
开发语言:C/C++
更新时间:2019-09-02
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
linux c实现简单的https获取百度一下("www.baidu.com"), 忽略证书。 linux下编译: g https.cpp -lssl -lcrypto
linux c实现简单的https获取百度一下("www.baidu.com"), 忽略证书。 linux下编译: g https.cpp -lssl -lcrypto
#include <stdio.h> #include <iostream> #include <string.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/socket.h> #include <sys/types.h> #include <unistd.h> #include <arpa/inet.h> #include <netdb.h> #include <errno.h> #include <time.h> #include <openssl/ssl.h> #include <openssl/err.h> #include <openssl/rand.h> #include <openssl/crypto.h> #include <openssl/rand.h> int https_get_request_wait_rsp(const char *ip, const char *name, int portnumber, const char *get_str, char *rsp_str, int rsp_buf_len) { int sockfd = 0; int ret; char buffer[1024*1024]; int nbytes; char host_addr[256]; char request[1024]; int send, totalsend; int i; SSL *ssl; SSL_CTX *ctx; char server_ip[16] = {0}; struct hostent *host; struct in_addr addr; struct sockaddr_in servaddr; if(ip == NULL || strlen(ip) < 7) { if((host = gethostbyname(name)) == NULL ) { return -1; } memcpy(&addr.s_addr,host->h_addr_list[0],sizeof(addr.s_addr)); strcpy(server_ip,(char *)inet_ntoa(addr)); } else { strcpy(server_ip, ip); } /* 客户程序开始建立 sockfd描述符 */ if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { /*建立SOCKET连接 */ printf("socket fail..............\n"); return -1; } memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(portnumber); if(inet_pton(AF_INET, server_ip, &servaddr.sin_addr) <= 0) { printf("inet_pton error for %s\n", server_ip); close(sockfd); return -1; } /* 客户程序发起连接请求 */ if (connect(sockfd, (struct sockaddr *) (&servaddr), sizeof(struct sockaddr)) == -1) { printf( "Connect Error:%s\a\n", strerror(errno)); close(sockfd); return -1; } /*ssl init*/ SSL_library_init(); SSL_load_error_strings(); ctx = SSL_CTX_new(SSLv23_client_method()); if(ctx == NULL) { close(sockfd); return -1; } ssl = SSL_new(ctx); if(ssl == NULL) { close(sockfd); return -1; } /*把socket和SSL关联*/ ret = SSL_set_fd(ssl, sockfd); if(ret == 0) { close(sockfd); return -1; } ret = SSL_connect(ssl); if(ret != 1) { close(sockfd); return -1; } sprintf(request, "GET /%s HTTP/1.1\r\nAccept: */*\r\nAccept-Language: zh-cn\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)\r\nHost: %s:%d\r\nConnection: Keep-alive\r\n\r\n", get_str, server_ip, portnumber); printf("%s", request); send = 0; totalsend = 0; nbytes = strlen(request); while(totalsend < nbytes) { send = SSL_write(ssl, request totalsend, nbytes - totalsend); if(send == -1) { close(sockfd); return -1; } totalsend = send; } printf("44444444:%d\n", totalsend); i = 0; /* while((nbytes = SSL_read(ssl, buffer, 1)) == 1) { printf("buffer:%s\n", buffer); if(i < 4) { if(buffer[0] == 'r' || buffer[0] == '\n') { i ; if(i>=4) { break; } } else { i = 0; } } }*/ printf("5555555\n"); memset(rsp_str, 0, rsp_buf_len); ret = SSL_read(ssl, rsp_str, rsp_buf_len); if(ret < 0) { printf("response ret =%d ======\n", ret); close(sockfd); return -1; } printf("response ret =%d=====>\t\t[%s]\n\n", ret , rsp_str); /*end ssl*/ SSL_shutdown(ssl); close(sockfd); SSL_free(ssl); SSL_CTX_free(ctx); ERR_free_strings(); return 0; } int main(void) { //char *hostname, *portnum, *weburl; char rsp_buf[40960] = {0}; https_get_request_wait_rsp(NULL, "www.baidu.com", 443, "", rsp_buf, sizeof(rsp_buf)); printf("=======>\n"); return 0; }