如何使用C建立HTTP连接?

如何使用C建立HTTP连接?,c,http,comet,C,Http,Comet,如何使用C建立HTTP连接?有参考资料或示例代码吗?此外,如何实现从用C编写的客户端打开Comet连接?(任何关于打开HTTPS连接的其他信息也将不胜感激。)谢谢 您需要库,例如,等等。试试这个,一个很好的参考 上面是socket编程教程,您可以使用它们构建自己的http客户机,否则您可以选择像curl这样的内置库。 如果使用普通套接字,则需要对每个请求的头信息进行编码和解码,还需要考虑http协议的许多其他因素 这是一个古老的话题,但肯定是谷歌经常搜索到的话题。在我自己研究了好几个星期之后,

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

您需要库,例如,等等。

试试这个,一个很好的参考

上面是socket编程教程,您可以使用它们构建自己的http客户机,否则您可以选择像curl这样的内置库。 如果使用普通套接字,则需要对每个请求的头信息进行编码和解码,还需要考虑http协议的许多其他因素

这是一个古老的话题,但肯定是谷歌经常搜索到的话题。在我自己研究了好几个星期之后,阅读了关于这个主题的文章,并复习了一些复杂的例子和教程。我把它浓缩到使它工作所需的最低限度

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

下面的代码也是我构建的一个更大项目的一部分,您可以查看hear,在那里可以找到如何安装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;
}
#包括
#包括
#包括
#包括
#定义APIKEY“您的API\U密钥”
#定义主机“您的WEB服务器URI”
#定义端口“443”
int main(){
//
//初始化变量
//
生物*生物;
SSL*SSL;
SSL_CTX*CTX;
//
//注册可用的SSL/TLS密码和摘要。
//
//基本上启动安全层。
//
SSL_库_init();
//
//创建一个新的SSL_CTX对象作为建立TLS/SSL的框架
//或启用DTLS的连接
//
ctx=SSL_ctx_new(SSLv23_client_method());
//
//->错误检查
//
如果(ctx==NULL)
{
printf(“Ctx为空\n”);
}
//
//创建由SSL BIO组成的新BIO链
//
bio=bio\U新\U ssl\U连接(ctx);
//
//使用字符串名称设置主机名
//
BIO_设置_连接_主机名(BIO,主机):“端口”;
//
//尝试连接提供的BIO
//
如果您正在寻找(BIO)连接(BIO)。