c - 在c中通过https发布json数据

标签 c json post openssl

我找到了以下代码片段并做了一些更改以将 json 数据发布到 re_url 。但它显示错误连接失败...服务器在 8888 端口上监听 json 数据。

#include <stdio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/bio.h>

#define APIKEY "YOUR_API_KEY"
#define HOST "https://YOUR_WEB_SERVER_URI"
#define PORT "8888"
char *s = "somejson";
int main() {

//
//  Initialize the variables
//
BIO* bio;
SSL* ssl;
SSL_CTX* ctx;

//
//   Registers the SSL/TLS ciphers and digests.
//
//   Basically start the security layer.
//
SSL_library_init();

//
//  Creates a new SSL_CTX object as a framework to establish TLS/SSL
//  or DTLS enabled connections
//
ctx = SSL_CTX_new(SSLv23_client_method());

//
//  -> Error check
//
if (ctx == NULL)
{
    printf("Ctx is null\n");
}

//
//   Creates a new BIO chain consisting of an SSL BIO
//
bio = BIO_new_ssl_connect(ctx);

//
//  Use the variable from the beginning of the file to create a 
//  string that contains the URL to the site that you want to connect
//  to while also specifying the port.
//
BIO_set_conn_hostname(bio, HOST ":" PORT);

//
//   Attempts to connect the supplied BIO
//
if(BIO_do_connect(bio) <= 0)
{
    printf("Failed connection\n");
    return 1;
}
else
{
    printf("Connected\n");
}

//
//  The bare minimum to make a HTTP request.
//
char* write_buf = "POST / HTTP/1.1\r\n"
                  "Host: " HOST "\r\n"
                  "Authorization: Basic " APIKEY "\r\n"
                  "Connection: close\r\n"
                  "\r\n";

//
//   Attempts to write len bytes from buf to BIO
//
if(BIO_write(bio, write_buf, strlen(write_buf)) <= 0)
{
    //
    //  Handle failed writes here
    //
    if(!BIO_should_retry(bio))
    {
        // Not worth implementing, but worth knowing.
    }

    //
    //  -> Let us know about the failed writes
    //
    printf("Failed write\n");
}

//
//  Variables used to read the response from the server
//
int size;
char buf[1024];

//
//  Read the response message
//
for(;;)
{
    //
    //  Get chunks of the response 1023 at the time.
    //
    size = BIO_read(bio, buf, 1023);

    //
    //  If no more data, then exit the loop
    //
    if(size <= 0)
    {
        break;
    }

    //
    //  Terminate the string with a 0, to let know C when the string 
    //  ends.
    //
    buf[size] = 0;

    //
    //  ->  Print out the response
    //
    printf("%s", buf);
}

//
//  Clean after ourselves
//
BIO_free_all(bio);
SSL_CTX_free(ctx);

return 0;
}

上面的代码将详细解释如何与远程服务器建立TLS连接。

重要说明:此代码不检查公钥是否由有效授权机构签名。这意味着我不使用根证书进行验证。不要忘记执行此检查,否则您将不知道您是否连接了正确的网站

当涉及到请求本身时。无非就是手写HTTP请求。

您还可以在此链接下找到有关如何在系统中安装 openSSL 以及如何编译代码以使其使用安全库的说明。

最佳答案

来自 BIO_set_conn_hostname :

BIO_set_conn_hostname() uses the string name to set the hostname. The hostname can be an IP address. The hostname can also include the port in the form hostname:port . It is also acceptable to use the form "hostname/any/other/path" or "hostname:port/any/other/path".

您没有提供有效的主机名:

#define HOST "https://YOUR_WEB_SERVER_URI"
...
//  Use the variable from the beginning of the file to create a 
//  string that contains the URL to the site that you want to connect
//  to while also specifying the port.
BIO_set_conn_hostname(bio, HOST ":" PORT);

评论正确地引用了一个 URL,但您提供的 URI 很可能是错误的。

如果将 YOUR_WEB... 替换为真实的主机名,它仍然是 URI 而不是主机名。 尝试删除 “https://” 部分并仅提供主机名。

关于c - 在c中通过https发布json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46947380/

相关文章:

c - 读取 int 值并将其分配给结构字段

ios - 从 completionHandler 返回 NSMutableArray( Objective-C )

c - gdb - 如何为指针数组调用 memset

c - 编译器如何为程序分配内存?

c - C 中连续两次 printf() 调用的奇怪行为

jquery - 在 JQuery 中序列化我的 JSON 输出问题

ios - 如何以 JSON 格式返回 Glassdoor API

javascript - 如何将类 JSON(不是 JSON)字符串转换为对象?

ios - 将 JSON 从 iOS 发布到 ASP.NET

ios - Swift 参数中的 POST 需要符号 "&"