【问题标题】:How to make a HTTP connection using C?如何使用 C 建立 HTTP 连接?
【发布时间】:2012-03-12 14:02:31
【问题描述】:

如何使用 C 建立 HTTP 连接?有任何参考资料或示例代码吗?此外,如何实现从用 C 编写的客户端打开 Comet 连接? (有关打开 HTTPS 连接的任何其他信息也将不胜感激。)谢谢!

【问题讨论】:

标签: c http comet


【解决方案1】:

试试这个,很好的参考

http://beej.us/guide/bgnet/

以上是socket编程的教程,你可以使用这些教程来构建自己的http客户端,或者你可以选择像curl这样的内置库。 如果您使用普通套接字,那么您需要对每个请求的标头信息进行编码和解码,还需要考虑有关 http 协议的许多其他因素 http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

【讨论】:

    【解决方案2】:

    这是一个古老的话题,但肯定是在 Google 上搜索了很多。经过数周我自己的研究,阅读了这个主题并阅读了一些复杂的示例和教程。我将其归结为使其工作所需的最低限度。

    重要提示:此代码不检查公钥是否由有效机构签名。这意味着我不使用根证书进行验证。

    下面的代码也是我构建的一个更大项目的一部分,您可以查看README.md 听到在哪里可以找到有关如何安装 openSSL 以及如何编译代码的更多信息。

    #include <stdio.h>
    #include <openssl/ssl.h>
    #include <openssl/err.h>
    #include <openssl/bio.h>
    
    #define APIKEY "YOUR_API_KEY"
    #define HOST "YOUR_WEB_SERVER_URI"
    #define PORT "443"
    
    int main() {
    
        //
        //  Initialize Variables
        //
        BIO* bio;
        SSL* ssl;
        SSL_CTX* ctx;
    
        //
        //   Registers the available SSL/TLS ciphers and digests.
        //
        //   Basically start the security layer.
        //
        SSL_library_init();
    
        //
        //  Creates a new SSL_CTX object as 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);
    
        //
        //  uses the string name to set the hostname
        //
        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");
        }
    
        //
        //  Data to send to create 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 write here
            //
            if(!BIO_should_retry(bio))
            {
                // Not worth implementing, but worth knowing.
            }
    
            //
            //  -> Let us know about the failed write
            //
            printf("Failed write\n");
        }
    
        //
        //  Variables used to read the response from the server
        //
        int size;
        char buf[1024];
    
        //
        //  Read the response message
        //
        for(;;)
        {
            //
            //  Put response in a buffer of size.
            //
            size = BIO_read(bio, buf, 1023);
    
            //
            //  If no more data, then exit the loop
            //
            if(size <= 0)
            {
                break;
            }
    
            //
            //  Terminate the string with a 0 so the system knows where
            //  the end is.
            //
            buf[size] = 0;
    
            printf("%s", buf);
        }
    
        //
        //  Clean after ourselves
        //
        BIO_free_all(bio);
        SSL_CTX_free(ctx);
    
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      你需要库,比如libcurlopenssl

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-18
        • 2011-03-01
        • 1970-01-01
        相关资源
        最近更新 更多