//SSL-Client.c
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
 
#define FAIL    -1
 
int OpenConnection(const char *hostname, int port)
{   int sd;
    struct hostent *host;
    struct sockaddr_in addr;
 
    if ( (host = gethostbyname(hostname)) == NULL )
    {
        perror(hostname);
        abort();
    }
    sd = socket(PF_INET, SOCK_STREAM, 0);
    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = *(long*)(host->h_addr);
    if ( connect(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
    {
        close(sd);
        perror(hostname);
        abort();
    }
    return sd;
}
 
SSL_CTX* InitCTX(void)
{   SSL_METHOD *method;
    SSL_CTX *ctx;
 
    OpenSSL_add_all_algorithms();  /* Load cryptos, et.al. */
    SSL_load_error_strings();   /* Bring in and register error messages */
    method = SSLv3_client_method();  /* Create new client-method instance */
    ctx = SSL_CTX_new(method);   /* Create new context */
    if ( ctx == NULL )
    {
        ERR_print_errors_fp(stderr);
        abort();
    }
    return ctx;
}
 
void ShowCerts(SSL* ssl)
{   X509 *cert;
    char *line;
 
    cert = SSL_get_peer_certificate(ssl); /* get the server's certificate */
    if ( cert != NULL )
    {
        printf("Server certificates:\n");
        line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
        printf("Subject: %s\n", line);
        free(line);       /* free the malloc'ed string */
        line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
        printf("Issuer: %s\n", line);
        free(line);       /* free the malloc'ed string */
        X509_free(cert);     /* free the malloc'ed certificate copy */
    }
    else
        printf("No certificates.\n");
}
 
int main(int count, char *strings[])
{   SSL_CTX *ctx;
    int server;
    SSL *ssl;
    char buf[1024];
    int bytes;
    char *hostname, *portnum;
 
    if ( count != 3 )
    {
        printf("usage: %s <hostname> <portnum>\n", strings[0]);
        exit(0);
    }
    SSL_library_init();
    hostname=strings[1];
    portnum=strings[2];
 
    ctx = InitCTX();
    server = OpenConnection(hostname, atoi(portnum));
    ssl = SSL_new(ctx);      /* create new SSL connection state */
    SSL_set_fd(ssl, server);    /* attach the socket descriptor */
    if ( SSL_connect(ssl) == FAIL )   /* perform the connection */
        ERR_print_errors_fp(stderr);
    else
    {   char *msg = "Hello???";
 
        printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
        ShowCerts(ssl);        /* get any certs */
        SSL_write(ssl, msg, strlen(msg));   /* encrypt & send message */
        bytes = SSL_read(ssl, buf, sizeof(buf)); /* get reply & decrypt */
        buf[bytes] = 0;
        printf("Received: \"%s\"\n", buf);
        SSL_free(ssl);        /* release connection state */
    }
    close(server);         /* close socket */
    SSL_CTX_free(ctx);        /* release context */
    return 0;
}
 
PS:个人觉得这个可能会中文会乱码

C调用OPENSSL做REST服务客户端的例子的更多相关文章

  1. 用PHP做服务器接口客户端用http协议POST访问安全性一般怎么做

    我的问题是,如果不做安全相关处理的话,一些可能改变数据库的操作可能会遭遇垃圾数据提交什么的,毕竟要找到这些信息只要找个http包就可以了 系统无用户登录 新手问题(从来没做过服务端开发),如果可以,给 ...

  2. 用Nacos做微服务架构里的服务注册与发现中心

    转自:https://www.jianshu.com/p/61608ff86344 Nacos 另一个非常重要的特性就是服务注册与发现,说到服务的注册与发现相信大家应该都不陌生,在微服务盛行的今天,服 ...

  3. Spring Cloud(三):Web服务客户端之Feign

    前文介绍了实现客户端负载均衡的Ribbon,但直接使用Ribbon的API来实现服务间的调用相对较为繁琐,服务间的调用能否像本地接口调用一样便捷.透明,更符合编程习惯呢?Feign就是用来干这事的. ...

  4. 微服务优化之使用gRPC做微服务的内部通信

    使用gRPC做微服务的内部通信 gRPC是一个由Google开源的远程服务调用框架,具有多路复用和双向流式通信的特性. 大家好,在本文中将为大家介绍为什么我们应该使用gRPC代替RESTful或JSO ...

  5. jquery 直接调用 wcf,面向服务的SOA架构 ( 第二天)

    在前面的基础上,我们来开始第二天编写 客户端 的东西,不过讲之前,我想告诉大家的是: 这个简单的SOA的架构,我们直接通过wcf 调用到 后台的方法, 而中间没有使用 C#代码,大大减少我们客户端的代 ...

  6. 跨语言调用Hangfire定时作业服务

    跨语言调用Hangfire定时作业服务 背景 Hangfire允许您以非常简单但可靠的方式执行后台定时任务的工作.内置对任务的可视化操作.非常方便. 但令人遗憾的是普遍都是业务代码和hagnfire服 ...

  7. Ace教你一步一步做Android新闻客户端(一)

    复制粘贴了那么多博文很不好意思没点自己原创的也说不出去,现在写一篇一步一步教你做安卓新闻客户端,借此机会也是让自己把相关的技术再复习一遍,大神莫笑,专门做给新手看. 手里存了两篇,一个包括软件视图 和 ...

  8. mutt+msmtp做linux邮件客户端

    mutt+msmtp做linux邮件客户端 1. 安装配置msmtp l  安装 wget https://sourceforge.net/projects/msmtp/files/msmtp/1.4 ...

  9. android 不同进程间的调用 AIDL 实现通讯服务

    android 不同进程间的调用  AIDL 实现通讯服务       近期对aidl android 不同进程间的调用,不同运用间的调用做了一些尝试: 过程例如以下: 1:首先在要被调用的程序里写好 ...

随机推荐

  1. DevExpress的 ASPxGridview控件的自动配置效果

    [淘宝小店:http://shop107919332.taobao.com ] 软件运行界面.是不是很漂亮? 这里是系统配置页面,可以随时根据需要配置每张表的每个字段的显示顺序,可见与否,只读与否,编 ...

  2. [04] SQL语句优化之索引

    1.索引的概念 根据书的目录可以知道内容所在的页码,不用一页一页翻书,可直接通过页码找到内容.数据库的索引类似于书本的目录,索引指向内容存储位置,可直接定位到内容而不必扫描整张表,减少了磁盘的I/O次 ...

  3. ios 经典错误

    1 - [person test]:unrecognized selector sent to instance. 给penson对象发送一个不能识别的消息:test   2 set/get方法死循环 ...

  4. 输出一个等边三角形的字母阵,等边三角形的两腰为字母A,往里靠依次字母大一个(详细题目文章中描述)

    题目简单的描述就是输出这么一个金字塔型的字母阵(等边三角形) /* A ABA ABCBA */ /* //解法①:对称轴法 #import <stdio.h> int main() { ...

  5. checkbox prop()函数

    1.设置checkbox选中状态 ①选中: .prop('checked',true); ②不选中:.prop('checked',false); 2.获取checkbox选中状态 .prop('ch ...

  6. mysql大数据高并发处理

    一.数据库结构的设计 如果不能设计一个合理的数据库模型,不仅会增加客户端和服务器段程序的编程和维护的难度,而且将会影响系统实际运行的性能.所以,在一个系统开始实施之前,完备的数据库模型的设计是必须的. ...

  7. BrowserSync:跨浏览器实时同步预览

    BrowserSync:跨浏览器实时同步预览 2016.09.11 官方网站:https://www.browsersync.io/ 项目仓库:https://github.com/Browsersy ...

  8. jQuery中模拟用户操作

    有时为了节省不想手动操作网页,但又想看到用户操作时的效果,可以用到jQuery提供的trigger方法.见下图代码 在不点击按钮时仍然想弹出this.value 我们只需要在后面加上.trigger( ...

  9. js清空web用户控件的值

    假设你的用户控件里面有: <asp:DropDownList ID="DropDownList1" runat="server"> <asp: ...

  10. jQuery.ajax()的一些例子

    例子: Example: 保存数据到服务器,成功时显示信息. 1 2 3 4 5 6 7 $.ajax({ method: "POST", url: "some.php& ...